Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • infix. import txt. Help with Importing Fixed-Width .txt File in Stata

    Hello everyone,

    I’m trying to import a fixed-width .txt file into Stata, where the variables do not have separators. Here is the code I am using:

    Code:
    clear  
    infix ///  
        codice_impresa 1-9 ///  
        codice_fiscale 10-36 str27 ///  
        ragione_sociale 37-236 str200 ///  
        indirizzo 237-316 str80 ///  
        cap 317-321 str5 ///  
    using "C:\Users\chiara.tasselli\OneDrive - mef.gov.it\Istat ASIA\Registro_Asia_Imprese_Anno_2021_anagrafica\MICRODATI\20593.IST133.A.2021.1.txt"
    I’ve already successfully imported a similar database, so I thought this would work. Interestingly, importing only the first variable works:
    Code:
    clear  
    infix ///  
        codice_impresa 1-9 ///  
    using "C:\Users\chiara.tasselli\OneDrive - mef.gov.it\Istat ASIA\Registro_Asia_Imprese_Anno_2021_anagrafica\MICRODATI\20593.IST133.A.2021.1.txt"
    The lengths and definitions of the variables are based on metadata, and I can't seem to identify the issue. Could anyone help diagnose the problem or suggest alternative methods for importing this type of .txt?

    Thank you for your time and assistance!

  • #2
    Put prefix str before variable name and try:
    Code:
    clear  
    infix ///  
        codice_impresa 1-9 ///  
        str27 codice_fiscale 10-36 ///  
        str200 ragione_sociale 37-236 ///  
        str80 indirizzo 237-316 ///  
        str5 cap 317-321 ///  
    using "C:\Users\chiara.tasselli\OneDrive - mef.gov.it\Istat ASIA\Registro_Asia_Imprese_Anno_2021_anagrafica\MICRODATI\20593.IST133.A.2021.1.txt"

    Comment


    • #3
      Originally posted by Chen Samulsion View Post
      Put prefix str before variable name and try:
      Code:
      clear
      infix ///
      codice_impresa 1-9 ///
      str27 codice_fiscale 10-36 ///
      str200 ragione_sociale 37-236 ///
      str80 indirizzo 237-316 ///
      str5 cap 317-321 ///
      using "C:\Users\chiara.tasselli\OneDrive - mef.gov.it\Istat ASIA\Registro_Asia_Imprese_Anno_2021_anagrafica\MICRODATI\20593.IST133.A.2021.1.txt"
      Thank you so much! Your advice was incredibly helpful, and now it works.
      Could you please explain why the code I used for another database worked anyway? Here's the code:
      clear
      Code:
                     infix ///                                codice_impresa 1-9 ///                                codice_regione 10-11 str2 ///                                codice_provincia 12-14 str3 ///                                codice_comune 15-17 str3 ///                                sigla_provincia 18-19 str2 ///                                denominazione_comune 20-69 str50 ///                                data_inizio_attivita_ 70-77 str8 ///                                data_fine_attivita_ 78-85 str8 ///                                forma_giuridica 86-89 str4 ///                                numero_indipendenti_media_ 90-97 ///                                occupati_dipendenti_media_ 98-108 ///                                addetti_me2 109-119 ///                                flag_attiva_almeno_sei_mesi 120 str1 ///                                flag_completezza_indirizzo 121 str1 ///                                flag_artigiana 122 str1 ///                                classe_di_fatturato 123-124 str2 ///                                ateco 125-129 str5 ///                                CLASSE_ETA_IMPRESA 130-134 ///                 using "C:\Users\chiara.tasselli\OneDrive - mef.gov.it\Istat ASIA\Registro_Asia_Imprese_Anno_2021_struttura\MICRODATI\20593.IST133.A.2021.1.txt"
      I also have another question: is it possible that everything works, but the first variable (which is numeric : codice_impresa) was imported with a shorter length?
      The metadata for this variable was: position 1, length 9. The numbers I see inside don’t seem to match.

      Thanks again for your very useful help!

      Comment


      • #4
        The standard infix grammar of specifications is: [byte|int|float|long|double|str] varlist [#:]#[-#], that is to say, you should put them in order of (1) variable storage type prefix, (2) variable name, (3) column range, for example
        Code:
        infix str22 state 1-22 int area 23-27 double pop_total 28-35 using popst6.raw, clear
        If you check your working code, you will find that according the above grammar, every variable storage type prefix was applied to the variable in the next line.

        Code:
        infix ///
            codice_impresa 1-9 ///
            codice_regione 10-11 str2 ///
            codice_provincia 12-14 str3 ///  
            codice_comune 15-17 str3 ///  
            sigla_provincia 18-19 str2 ///
            denominazione_comune 20-69 str50 ///
            data_inizio_attivita_ 70-77 str8 ///
            data_fine_attivita_ 78-85 str8 ///  
            forma_giuridica 86-89 str4 ///  
            numero_indipendenti_media_ 90-97 ///  
            occupati_dipendenti_media_ 98-108 ///
            addetti_me2 109-119 /// 
            flag_attiva_almeno_sei_mesi 120 str1 ///
            flag_completezza_indirizzo 121 str1 ///
            flag_artigiana 122 str1 ///
            classe_di_fatturato 123-124 str2 /// 
            ateco 125-129 str5 ///
            CLASSE_ETA_IMPRESA 130-134 ///
            using "C:\Users\chiara.tasselli\OneDrive - mef.gov.it\Istat ASIA\Registro_Asia_Imprese_Anno_2021_struttura\MICRODATI\20593.IST133.A.2021.1.txt"

        Comment


        • #5
          Chen Samulsion , thank you for your suggestions. They have been incredibly helpful!

          Comment

          Working...
          X