DATA Step, Macro, Functions and more

Keep/Drop variables based on one variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 76
Accepted Solution

Keep/Drop variables based on one variable

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

 


Accepted Solutions
Solution
‎07-18-2016 04:02 PM
Super User
Posts: 19,791

Re: Keep/Drop variables based on one variable

Posted in reply to Xiaoningdemao

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;

View solution in original post


All Replies
Respected Advisor
Posts: 4,920

Re: Keep/Drop variables based on one variable

Posted in reply to Xiaoningdemao

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?

PG
Frequent Contributor
Posts: 76

Re: Keep/Drop variables based on one variable

Hi PG,
I meant the variable 'keep' stores the variable names I want to keep, 'drop' stores the variables I want to drop, we only need to use one of it. But i don't know which is easier to use. @PG Stats@PG Stats
Frequent Contributor
Posts: 76

Re: Keep/Drop variables based on one variable

Posted in reply to Xiaoningdemao
And since the 1000 variables we have here has both numerical and character variables I cannot simply transpose it.
Super User
Posts: 11,343

Re: Keep/Drop variables based on one variable

Posted in reply to Xiaoningdemao

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.

Frequent Contributor
Posts: 76

Re: Keep/Drop variables based on one variable

hi dallardw,
But I did put keep and drop in one data step before, i didn't recall any weird result though.... O_O

Best,
Solution
‎07-18-2016 04:02 PM
Super User
Posts: 19,791

Re: Keep/Drop variables based on one variable

Posted in reply to Xiaoningdemao

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;
Frequent Contributor
Posts: 76

Re: Keep/Drop variables based on one variable

Dear Reeza,
Thank you very much! This is exactly what I want!!
Best,
Super User
Posts: 19,791

Re: Keep/Drop variables based on one variable

Posted in reply to Xiaoningdemao
Frequent Contributor
Posts: 76

Re: Keep/Drop variables based on one variable

No, I didn't. But this is very helpful as well! Thanks again.

Best,
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 365 views
  • 1 like
  • 4 in conversation