BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

9 REPLIES 9
PGStats
Opal | Level 21

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
Xiaoningdemao
Quartz | Level 8
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
Xiaoningdemao
Quartz | Level 8
And since the 1000 variables we have here has both numerical and character variables I cannot simply transpose it.
ballardw
Super User

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.

Xiaoningdemao
Quartz | Level 8
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,
Reeza
Super User

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;
Xiaoningdemao
Quartz | Level 8
Dear Reeza,
Thank you very much! This is exactly what I want!!
Best,
Xiaoningdemao
Quartz | Level 8
No, I didn't. But this is very helpful as well! Thanks again.

Best,

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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