05-19-2016 09:38 PM
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.
set data (keep = :123 ); -- I try to do this, but it did not work.
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.
05-19-2016 10:34 PM
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.
05-19-2016 11:09 PM - edited 05-19-2016 11:56 PM
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.
05-20-2016 02:02 AM
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.