jeffrey
jeffrey

Reputation: 2096

Local macro on subsample data using if statement in Stata

I want to use the local command in Stata to store several variables that I afterwards want to export as two subsamples. I separate the dataset by the grouping variable grouping_var, which is either 0 or 1. I tried:

if grouping_var==0 local vars_0 var1 var2 var3 var4
preserve
keep `vars_0'
saveold "data1", replace
restore

if grouping_var==1 local vars_1 var1 var2 var3 var4
preserve
keep `vars_1'
saveold "data2", replace
restore

However, the output is not as I expected and the data is not divided into two subsamples. The first list includes the whole dataset. Is there anything wrong in how I use the if statement here?

Upvotes: 0

Views: 771

Answers (1)

Brendan
Brendan

Reputation: 4011

There is a bit of confusion between the "if qualifier" and the "if command" here. The syntax if (condition) (command) is the "if command", and generally does not provide the desired behavior when written using observation-level logical conditions.

In short, Stata evaluates if (condition) for the first observation, which is why your entire data set is being kept/saved in the first block (i.e., in your current sort order, grouping_var[1] == 0). See http://www.stata.com/support/faqs/programming/if-command-versus-if-qualifier/ for more information.

Assuming you want to keep different variables in each case, something like the code below should work:

local vars_0 var1 var2 var3 var4
local vars_1 var5 var6 var7 var8
forvalues g = 0/1 {
    preserve
        keep if grouping_var == `g'
        keep `vars_`g''
        save data`g' , replace
    restore
}   

Upvotes: 3

Related Questions