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

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
  • 5 replies
  • 1452 views
  • 4 likes
  • 4 in conversation