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.
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.
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.
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
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!
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.
Ready to level-up your skills? Choose your own adventure.