Announcement

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

  • Dictionary files and loops?

    I have 9 .txt files containing data, one for each year from 2010 to 2018. All files share the same naming convention; the only thing that differs is the year. These data files require a dictionary to be read in. Each file needs the same dictionary; however, I need to specify which file is being read in at the beginning of the dictionary file and I don't see a way to loop. Is there a way to do this elegantly--i.e., without having to create a separate .dct file for each data file? Ultimately, I'd like to read in the first file, 2010, using the dictionary, save the data as a .dta file, then do the same for the subsequent years. Then, I would like to append all the .dta files.

    From my .do file:

    Code:
    infile using "S:\Project\CDC_Dictionary.dct"
    My dictionary file (there are many more variables than this, but I cut most out to save space):
    Code:
    dictionary using "S:\Project\Mort2010US.txt" {
        
        str39  race %39s                 "Race"
        str5   hisp %5s                  "Hispanic origin"
        str2   race_recode_40 %2s        "Race recode 40"
    
    }

  • #2
    If I coreectly understand the output of
    Code:
    help infile2
    specifying the input file in the dictionary is not required, and the using() option on the infile command will allow you to specify the input file.
    Code:
    infile using "S:\Project\CDC_Dictionary.dct", using("S:\Project\Mort2010US.txt")
    Code:
    dictionary {
        
        str39  race %39s                 "Race"
        str5   hisp %5s                  "Hispanic origin"
        str2   race_recode_40 %2s        "Race recode 40"
    
    }

    Comment


    • #3
      It sounds like you are looking for something like this:

      Code:
      forv year = 2010/2018{
          di "S:\Project\Mort`year'US.txt"
      }
      Although I can't test, the above implies something like this might work:

      Code:
      forv year = 2010/2018{
          infile using "S:\Project\CDC_Dictionary.dct"
      
          dictionary using "S:\Project\Mort`year'US.txt" {
          
              str39  race %39s                 "Race"
              str5   hisp %5s                  "Hispanic origin"
              str2   race_recode_40 %2s        "Race recode 40"
      
          }
      }
      Last edited by Daniel Schaefer; 28 Dec 2020, 12:37.

      Comment


      • #4
        forval i=1/20 {
        cap conf v dictionary_V2_level`i'
        if (_rc) continue
        infile using "C:\Users\Dropbox\PC\Desktop\Drop_box\dictiona ry\d ictionary_V2_level`i'.dct"
        save "C:\Users\Dropbox\PC\Desktop\Drop_box\dta2\dat a_v2 _level`i'", replace
        clear
        }
        This is what i have been using to extract data from dictionary files that wrote. However the problem is since i don't have consecutives values of levels in my dictionary files, this loop won't work. I tried inserting :
        cap conf v dictionary_V2_level`i'
        if (_rc) continue
        to solve this, but this did not work. Can someone suggest me how can i run this code and what is wrong it?
        Last edited by Anand dubey; 21 Feb 2023, 12:29.

        Comment

        Working...
        X