Dear All,
I have a question about drop / keep variables. Now I have such a data set:
age gender height keep drop
10 1 160 age gender
11 2 159 * height
8 1 161 * *
So the first three variables are the observation info, and the last two variables listed the variables that I want to kee/drop. So the desired fresult would be:
age
10
11
8
I can not use keep or drop because the actual data set contains almost 1000 variables, it is unrealistic to list them. I have been struggled with this for almost two days.
Thank you in advance!!
Best,
Thank
This is a really weird data structure.
I I would use a previous step to pull names into a macro variable and execute it. I do wonder if going back a few steps would help this process.
Proc SQL noprint;
Select keep into :keep_list separated by ' '
From have;
Quit;
Data want;
Set have;
Keep &keep_list;
Run;
No wonder you have been struggling. The first observation says keep age and drop gender. So where does height go?
The second observation: It says keep nothing and drop height. Yet age is kept. And where does gender end up?
How is this supposed to work? How will you know what the values kept actually are?
Keep and Drop are not conditional. They affect the entire structure of a data set. A variable that is dropped is removed entirely from the dataset. If you use Keep, then only the referenced data is kept. If you use more than one Keep then all variables that appear on any keep statement are in the final data:
Keep age;
Keep height;
means the only variables in the result set will be age and height.
And from the online help for the Keep statement:
Note: Do not use both the KEEP and DROP statements within the same DATA step.
It may be easier to use WHERE clauses in other steps to select records with desired properties. With 1000 variables in your data that may be difficult.
This is a really weird data structure.
I I would use a previous step to pull names into a macro variable and execute it. I do wonder if going back a few steps would help this process.
Proc SQL noprint;
Select keep into :keep_list separated by ' '
From have;
Quit;
Data want;
Set have;
Keep &keep_list;
Run;
Your also aware of variable shortcut lists I assume?
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.