BookmarkSubscribeRSS Feed
ursula
Pyrite | Level 9

Hi,

there are some variables in my dataset that have the same name at the last 3 letters.

For example:  ABC_123, BCD_123, CBB_123, GHJ_123, --- and more

I wonder how to code the simple way in the "keep" statement in order to keep all the variables.

 

For example:

data want;

set data (keep = :123 ); -- I try to do this, but it did not work.

run;

 

if only a small amount of variable, I defenitely can just write the full name of the variable (keep=ABC_123 BCD_123), but if I want more variables with the same 3 last letters, the "keep" statement will be very long.

 

is it a way to simplify the code?

 

can anyone show me how to code it?

 

Thank you very much in advance.

5 REPLIES 5
PGStats
Opal | Level 21

SAS data step language doesn't have syntax to create variable lists from name suffixes. However, if your variables appear consecutively in the dataset variable list, you can use a list such as ABC_123 -- GHJ_123 which means all the variables between ABC_123 and GHJ_123. Note the double dashes between the first and last variable names.

PG
ChrisNZ
Tourmaline | Level 20

No SAS syntax for this, the usual way is to extract the names :

 

proc sql noprint;
   select NAME into :var_list separated by ' '
    from dictionary.COLUMNS 
    where LIBNAME ='MYLIB' 
      and MEMNAME ='MYTAB'
      and strip(NAME) like '%123';
quit;                 
data WANT;
  set MYLIB.MYTAB(keep= &var_list);
run;

Be careful about the case if your suffix is alphabetical.

 

ursula
Pyrite | Level 9
Thank you for the good idea.
I think I can group them together first and then run the code👍
ursula
Pyrite | Level 9
Thank you chris for the code. This is a good option too.
BrunoMueller
SAS Super FREQ

Hi

 

Have a look at this blog entry http://blogs.sas.com/content/sastraining/2011/11/18/jedi-sas-tricks-building-a-name-suffix-variable-... by @SASJedi

 

Please make sure you also read the comments.

 

Bruno

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2571 views
  • 4 likes
  • 4 in conversation