Hello ,
I would like to add double quotes between macro varible string based on space deilimeter
this macro varible to use in where conditon in data set.
%let vars= VAR1 VAR2 VAR3 VAR4;
I would like to create another KEEP_VAR macro varible based on above macro varible.
%put KEEP_OBS = &KEEP_OBS;
KEEP_OBS= "VAR1" "VAR2' "VAR3" "VAR4" ;
So i will use KEEP_OBS in where condiation in data set like below.
data test1;
set test2;
where varname in (&KEEP_OBS);
run;
Thank you,
Raja.
%let vars= VAR1 VAR2 VAR3 VAR4; %let want="%sysfunc(prxchange(s/\s+/" "/,-1,&vars))"; %put %bquote(&want);
You can use the %QLIST macro
https://github.com/sasutils/macros/blob/master/qlist.sas
where varname in %qlist(&vars);
I point out that your question is quite confusing, you are using VAR which seems to indicate you are talking about variables, but then you go ahead and include in your code where varname in which will not select variables, it will select observations (if you do it properly).
Hi Paige Miller,
Thank you for response,
Yes , i am filetring the observations in a data set based on macro varible values based on space delimeterd.
is any other way to do without use %qlist macro.
Thank you,
Raja
@raja777pharma wrote:
Yes , i am filetring the observations in a data set based on macro varible values based on space delimeterd.
is any other way to do without use %qlist macro.
Sure, you could write your own macro, or custom code that works. But why, when %qlist works?
@raja777pharma wrote:
is any other way to do without use %qlist macro.
I don't know if you saw my earlier response, but:
%LET Keep_Obs = "%QSYSFUNC(TRANWRD(%QSYSFUNC(COMPBL(&Keep_Obs)), %STR( ),%STR(" ")))";
Jim
I'm sure this works, @jimbarbour , but to @raja777pharma: it is just soooooo much easier to type %qlist
I agree, although if one feels the need to understand the code, qlist is perhaps more complicated, and some of the comments don't make sense, at least to me.
Why is there a comment in front of &rp in this code? I see what &rp is doing, but I don't understand why that comment would be necessary, although I have not played with the code.
%*----------------------------------------------------------------------
Add parentheses when requested.
-----------------------------------------------------------------------;
%*;&rp
%mend qlist;
Jim
The macro is over complicated because it supports additional options not needed in this case.
I assume the empty comment is the one you don't understand?
It will prevent SAS from generating an extra space in front of the value of &rp. Without it you get something like
('a','b' )
instead of
('a','b')
There are other ways to suppress that leading space, but I find that one the easiest to code.
Ah. Interesting. It looked too deliberate to be a casual error. I figured there had to be some reason for it, but it wasn't immediately obvious. Thank you. That tidbit might actually come in handy some day.
Jim
The FINDW() function is useful.
You can use the i modifier to make the test case insensitive.
%let list=A b c D ;
...
where findw("&list",varname,' ','it')
...
Another option:
%LET Keep_Obs = "%QSYSFUNC(TRANWRD(&Keep_Obs, %STR( ),%STR(" ")))";
This assumes that there is only one space between each term in Keep_Obs.
If there were more than one space between each term, then one would code:
%LET Keep_Obs = "%QSYSFUNC(TRANWRD(%QSYSFUNC(COMPBL(&Keep_Obs)), %STR( ),%STR(" ")))";
Jim
%let vars= VAR1 VAR2 VAR3 VAR4; %let want="%sysfunc(prxchange(s/\s+/" "/,-1,&vars))"; %put %bquote(&want);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.