DATA Step, Macro, Functions and more

Select variables with a KEEP statement and a prompt

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

Select variables with a KEEP statement and a prompt

Hello !

Here's my problem : I've got a table with variables like TEST_Q3_07 TEST_Q4_07 [...] TEST_Q4_2012 THING_Q3_07 THING_Q4_07 [...] THING_Q4_2012

What I'd like is through a prompt and a KEEP statement allow the user to choose which set he'd like to keep.

My prompt would create a macro variable call &Variable and possible values would be TEST or THING and then my question is : how do I include a set of variables in a KEEP statement where's a prompt involved ?

Here's one of the codes I tried, unsuccessfully ;

DATA HISTO ; SET HISTO ;

KEEP=&Variable_Q3_07  &Variable_Q4_07 [...] &Variable_Q4_12;

run;

Thank you


Accepted Solutions
Solution
‎09-05-2013 11:46 AM
Super Contributor
Posts: 334

Re: Select variables with a KEEP statement and a prompt

did you try the code with the macro delimiter in the keep statement macro variables?

KEEP &Variable._Q3_07  &Variable._Q4_07 [...] &Variable._Q4_12;

by the way if you are using keep as part of the data step there is no = in the syntax. If you move to the set statement and use as a data set option Histo (keep= blah ); they you use the = and this might be a better option depending on the size of the data you are using.

See if that helps

EJ

View solution in original post


All Replies
Solution
‎09-05-2013 11:46 AM
Super Contributor
Posts: 334

Re: Select variables with a KEEP statement and a prompt

did you try the code with the macro delimiter in the keep statement macro variables?

KEEP &Variable._Q3_07  &Variable._Q4_07 [...] &Variable._Q4_12;

by the way if you are using keep as part of the data step there is no = in the syntax. If you move to the set statement and use as a data set option Histo (keep= blah ); they you use the = and this might be a better option depending on the size of the data you are using.

See if that helps

EJ

Contributor
Posts: 40

Re: Select variables with a KEEP statement and a prompt

Thanks a lot, the macro delimiter works fine and the result's exactly what I expected.

Sincerely

GV

Super User
Posts: 5,497

Re: Select variables with a KEEP statement and a prompt

I agree with all the recommendations so far, especially moving KEEP to the SET statement:

data histo;

set histo (keep=some list of variables);

run;

Also note ... if you want ALL variable names that begin with TEST or THING, you can abbreviate the code:

data histo;

set histo (keep=&variableSmiley Happy;

run;

The colon modifier (no space allowed before the colon) will automatically indicate that your list of variables includes all variable names that start with &VARIABLE

Good luck.

Contributor
Posts: 40

Re: Select variables with a KEEP statement and a prompt

Posted in reply to Astounding

Thanks Astounding ! It will make it better !

Sincerely

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 204 views
  • 4 likes
  • 3 in conversation