Home > Software engineering >  How to keep a variable if it exists
How to keep a variable if it exists

Time:05-20

Say that I have these two datasets: data1 and data2. They are the same, except that data1 includes the make variable while data2 does not.

sysuse auto2, clear
*create first dataset
preserve
tempfile data1
save `data1'
restore
*create second dataset
preserve
drop make
tempfile data2
save `data2'
restore

My problem is simple. I just want to write code so that I keep certain variables if they exist and keep a more limited set if not. Note that in my application, dropping variables isn't very feasible. I need to use keep.

In my toy example, I want to keep price and make for the datasets that have it (namely data1) and only price for the datasets that do not have make (namely data2).

This does not work because keep fails if a given variable is not in the dataset:

forvalues i=1/2 {
    use `data`i'', clear
    keep price make
}

Any suggestions? I wonder if there is some way to use an if else statement to test if the variable make exists and then to either execute keep price make or keep price. Or possibly some way to use capture keep price make and then based on if this fails or not, go to keep price. Would these work or any other methods?

CodePudding user response:

Here is one approach: get a list of the variables that are present and look for the intersection of those and the set of variables that are wanted.

Your tentative code does nothing to save the results. Here I have overwritten the dataset just read in; your application may (indeed should) wish to save it to a new file.

forvalues i=1/2 {
    use `data`i'', clear
    unab present : * 
    local wanted price make 
    local available : list present & wanted 
    keep `available' 
    save, replace 
}

See also e.g. isvar from SSC and especially capture confirm var.

  • Related