Announcement

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

  • Looping over files to create new datasets

    Hi. I have four files in a folder that have the following naming convention: "emr22-24_`x'.dta". I want to run a loop over them that does the following:

    1. generates a new binary variable (median_tele in the code below) based on the median of the variable "week_doctele"
    2. based on this new variable, creates two new datasets from each of the dataset: one for median_tele==1, and the other for median_tele==0
    3. name the dataset with median_tele==0 "emr22-24_`x'_low.dta"
    4. name the dataset with median_tele==1 "emr22-24_`x'_high.dta"

    Any help in doing so will be much appreciated!


    Code:
    local x MTC KVX GMRV KAR
    local files: dir "${db}\inputdata" files "emr22-24_`x'.dta"
    
    
    foreach file of local files {
            
            sum week_doctele, detail  
            gen median = r(p50)
            gen median_tele = (week_doctele>=median) 
                  
    }
    
    *sample from one of the datasets
    
    input long con_id float(week_num week_doctele)
    1   1       .25
    1   2       .45
    1   3       1.2
    1   4       .25
    1   5       .65
    1   6       .55
    1   8        .1
    1   9       .15
    1  11       .05
    1  12       .15
    1  14        .1
    1  16       .15
    2  44 .04545455
    2  46 .04545455
    2  50 .04545455
    2  54 .04761905
    2  55  .0952381
    2  98  .1904762
    2  99 .04761905
    2 100  .0952381
    2 101 .14285715
    2 102 .04761905
    2 104  .3809524
    2 105 .23809524
    2 107  .1904762
    2 108  .3333333
    2 110  .1904762
    2 111 .14285715
    2 112 .14285715
    2 113  .4285714
    2 114  .1904762
    3   1       .05
    3   2       .25
    3   4        .1
    3   5        .2
    3   6       .05
    3  11        .1

  • #2
    Code:
    foreach file of local files {
            
            sum week_doctele, detail  
            gen median = r(p50)
            gen median_tele = (week_doctele>=median)
            
            preserve
            keep if median_tele == 1
            save emr22-24_`x'_high, replace
            restore
            keep if median_tele == 0
            save emr22_24_`x'_low, replace
                  
    }

    Comment


    • #3
      Clyde Schechter: Thank you. That worked perfectly!

      Comment

      Working...
      X