Hi,
I need to exclude some variables from a self-defined variable list. that is,
%LET large=a b c d e f g h i j k;
%LET exclude=c d h k;
the variable list I want to have is a b e f g i j so that I can call in them for later steps.
In Stata, there is a function like:
local finalvar: list large - exclude
Then later on I can just call in finalvar.
is there any similar step in SAS?
Thanks in advance.
Thanks,
Sunny
%LET large=a b c d e f g h i j k;
%LET exclude=c d h k;
%let final=%sysfunc(compress(&large, %sysfunc(compress(&exclude.))));
%put final=&final;
%LET large=a b c d e f g h i j k;
%LET exclude=c d h k;
%let final=%sysfunc(compress(&large, %sysfunc(compress(&exclude.))));
%put final=&final;
This is so cool! Many thanks!
Be aware that this only works on characters as you presented, if you have strings, it gets more complicated.
If you want to work with variable lists in the context of variables and not simply a string of words you need some help from SAS. PROC TRANSPOSE although a seeming unlikely candidate is the perfect tool.
wow really appreciated!
%LET large=a b c d e f g h i j k;
%LET exclude=c d h k;
%let want=%sysfunc(prxchange(s/%sysfunc(translate(&exclude,%str(|),%str( )))//,-1,&large));
%put &want;
I think, It would work if its single word like a,b,c,d,e
What if the column names are age sex race. Compress will remove a from age and e from sex and a,c,e from race.
proc contents data=sashelp.class out=class;
run;
proc sql;
select name into :include separated by " " from class;
quit;
%put &include;
%let exclude=weight;
%let want=%sysfunc(compress(&include,%sysfunc(compress(&exclude))));
%put &want;
Can Anyone explain how to make a change to the below code to work for a string of words..I see a macro but don't understand it. Looking for some simple answer..Thanks.
How about :
%LET large=ab ba b cat d e f g h i j k;
%LET exclude=cat b h k;
%let want=%sysfunc(prxchange(s/\b%sysfunc(tranwrd(&exclude,%str( ),%str(\b|\b)))\b//,-1,&large));
%put &want;
Thanks Keshan. Much appreciated. I have never used prx functions. Do you mind explaining us, how it works! Thanks.
Oh. That is a very long story need to talk . You'd check the SAS documentation about PRX .
Or. You could ask Patrick who can explain it better than me .
Awesome. That worked..:) Much appreciated.
For those SAS users who are not familiar with (quite complex) PRX functions, here is a little piece of more classical SAS code I built for excluding some variables from a list of variables:
* extracting the variables' names list (these names are: Name Sex Age Height Weight);
ODS OUTPUT PositionShort=ps;
PROC CONTENTS DATA=sashelp.class VARNUM SHORT;
RUN;QUIT;
* creating a macrovariable containing this list;
DATA _NULL_;
SET ps;
CALL SYMPUT ('string', variables);
RUN;
* specifying the list of words (ie variables'names) to be excluded;
%LET exclude= Age Weight;
* how many words to be excluded;
%LET words_number=%SYSFUNC(COUNTW(&exclude));
* iteratively excluding undesirable words;
%MACRO m;
%DO number_in_list=1 %TO &words_number;
/* selects one word in turn */
%LET word=%SYSFUNC(SCAN(&exclude, &number_in_list));
/* excludes that word */
%LET string=%SYSFUNC(TRANWRD(&string, &word,));
%END;
%MEND;
%m;
%PUT &string;
Hope it is helpful
JC
cool! Thanks.
Hey. Did you get a solution for the above. If so, Please share it.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.