DATA Step, Macro, Functions and more

call the variables with the same 3 last letters

Reply
Contributor
Posts: 64

call the variables with the same 3 last letters

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.

Respected Advisor
Posts: 4,654

Re: call the variables with the same 3 last letters

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
PROC Star
Posts: 1,564

Re: call the variables with the same 3 last letters

[ Edited ]

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.

 

Contributor
Posts: 64

Re: call the variables with the same 3 last letters

Thank you for the good idea.
I think I can group them together first and then run the code👍
Contributor
Posts: 64

Re: call the variables with the same 3 last letters

Thank you chris for the code. This is a good option too.
SAS Super FREQ
Posts: 683

Re: call the variables with the same 3 last letters

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

Ask a Question
Discussion stats
  • 5 replies
  • 289 views
  • 4 likes
  • 4 in conversation