BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
raja777pharma
Fluorite | Level 6

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.

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
%let vars= VAR1 VAR2 VAR3 VAR4; 

%let want="%sysfunc(prxchange(s/\s+/" "/,-1,&vars))";


%put %bquote(&want);

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

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).

--
Paige Miller
raja777pharma
Fluorite | Level 6

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

 

 

PaigeMiller
Diamond | Level 26

@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?

--
Paige Miller
jimbarbour
Meteorite | Level 14

@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

PaigeMiller
Diamond | Level 26

I'm sure this works, @jimbarbour , but to @raja777pharma: it is just soooooo much easier to type %qlist

--
Paige Miller
jimbarbour
Meteorite | Level 14

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

Tom
Super User Tom
Super User

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.

jimbarbour
Meteorite | Level 14

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

Tom
Super User Tom
Super User

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')
...
jimbarbour
Meteorite | Level 14

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

Ksharp
Super User
%let vars= VAR1 VAR2 VAR3 VAR4; 

%let want="%sysfunc(prxchange(s/\s+/" "/,-1,&vars))";


%put %bquote(&want);

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 11 replies
  • 1094 views
  • 1 like
  • 5 in conversation