0

I want to loop through different files:

forvalues y=2014/2022 {
    use var1 var2 var3 using "file_`y'.dta"
    save "file2_`y'
}

However, some of the files have var names VAR1 VAR2 var3 instead of var1 var2 var3. How do I make the statement case insensitive?

4
  • for values won't work. Please fix it to forvalues or forval or something else legal. Commented Mar 20, 2023 at 18:04
  • If you have inconsistent variable names in different datasets, make them consistent first in a loop over datasets with statements like capture rename VAR1, lower. Otherwise with these datasets you will be writing awkward code to cope with the inconsistencies in everything that uses them henceforth. Commented Mar 20, 2023 at 18:08
  • @NickCox where would I put that capture code? As the first thing in the loop? Commented Mar 22, 2023 at 10:26
  • You need to read in the dataset. Then try your rename statements. You can't go use var1 var2 var3 etc. unless those are the names actually in use. Commented Mar 22, 2023 at 10:49

1 Answer 1

1

I'm not sure there's a concise way to do this, but here's an example using describe and capture to first check if the variable is contained in the .dta file.

First make the simple data examples with different cases:

tempfile the_data1 the_data2
clear
input var1 VAR2 var3
0 1 0
end 
save `the_data1', replace 

clear
input var1 var2 VAR3 // note uppercase is different from above
0 1 0
end 
save `the_data2', replace 

Then this loop handles the different names. The outer loop goes through the two files and creates an empty local myvarlist which will contain the appropriate variable names for that file. The capture line tries a describe command of the lowercase variable. If it works (_rc==0), the lowercase name is added to the list. If it fails (rc!=0), the uppercase name is added to the list. After looping through the variables, the use line works because the cases have all been corrected.

forvalues i=1/2 {
    local myvarlist ""
    foreach myvar in var1 var2 var3 {
        capture noisily describe `myvar' using `the_data`i'' 
        if (_rc==0) local myvarlist "`myvarlist' `myvar'"
        if (_rc!=0) local myvarlist = "`myvarlist' " + upper("`myvar'")
    }
    use `myvarlist' using `the_data`i''
    describe 
    * Save file here
}
Sign up to request clarification or add additional context in comments.

3 Comments

Helpful (+1) but please see my comment on the question.
Thanks @NickCox! I agree with your comment in general...I think I could see this as the least bad option if the raw .dta files are quite large and you want code that works on the raw files for reproducibility.
Good point! That could be crucial in some cultures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.