Announcement

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

  • aving with filename defined by value in a cell

    Saving with filename defined by value in a cell

    Hi,

    I have a dataset

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    
    clear
    
    input str18 bvd_id_number str2 countrycode
    
    "PT503338435" "PT"
    
    "MA37163-81" "MA"
    
    "JP1011001052846" "JP"
    
    "FI10958757" "FI"
    
    "US115044924GN" "US"
    
    "US112599986GN" "US"
    
    "IT03704400237" "IT"
    
    "IT01868030543" "IT"
    
    "HU14010040" "HU"
    
    "PT501850066" "PT"
    
    end






    I'm using a loop to split it into country sets (master dataset is approx 400gb .csv).




    the stata code I'm using is




    Code:
    
    
    
    egen group = group(countrycode)
    
    su group, meanonly
    
    foreach i of num 1/`r(max)' {
     preserve   keep if group == \`i'  save \`i'.dta  
    restore
    
    }



    This works fine to split the file into individual countries, but it saves the file as file1.dta, file2.dta, etc. This wouuld be ok, except I will need to append multiple files together to make a whole country dataset, and file1.dta is not always consistently AD (Andorra), it might be AE(United Arab Emirates), for example.

    How can I tell Stata to use any value from the countrycode variable that it has in memory to use as the filename?




    Thanks
    Last edited by Ciaran OFlynn; 09 Feb 2023, 09:55.

  • #2
    There is no need for the variable group here. If you need it for some other purpose, fine, but don't use it as the iterator in your loop.
    Code:
    levelsof countrycode, local(countries)
    foreach c of local countries {
        preserve
        keep if countrycode == "`c'"
        save `c', replace
        restore
    }
    Note: Assumes that the country codes do not contain any embedded blanks. If they do, then -save `"`c'.dta"', replace-. Also assumes the country codes do not contain any embedded quotation marks. If they do, -keep if countrycode == `"`c'"'-.
    Last edited by Clyde Schechter; 09 Feb 2023, 10:11.

    Comment


    • #3
      Super - thank you!

      Comment

      Working...
      X