Hi,
I have a macro &u = 6,22,5
1.) How do I detect from macro u where there is a comma %if scan(&x,',') > 0 will this work ?
2.) How do I count the number of words from the string u ( Answer should be )
3.) How do I extract 6 from string u and 22 from string u and assign that to macro variable and 5 from string u and assign it to macro variable
Please Can anyone help
Thanks
1. It's not clear what you want here
2. COUNTW/COUNT
3. %SCAN or SCAN
Hi
I have macro variable
%let u = 6,22,5
I want a macro code to check whether comma exists in the macro u if yes then I want a syntax to count macro u ( Answer should be 3 in above case )
next I want syntax to extract 6 into one macro
22 into one macro
5 into one macro variable
Thank you
I would use FIND or INDEX personally to see if there's a comma.
If you're having trouble with that feel free to post your code and log.
I did not find any way to use macro functions, to resolve your issue, that is - dealing with a list of values separated by comma.
You can either use a list of values separated by a blank/space and use method quoted by @Reeza
or you can use sas datastep to do it:
%let u = 6,22,5;
%macro test;
data _NULL_;
comma_position = index("&u",',');
count = countw("&u");
call symput('Comma_Position',left(comma_position));
call symput('Count',strip(count));
run;
%put First Comma is in position &comma_position;
%put The list {&u} contains &count Members;
%mend test;
%test;
Hi ,
Thank you this worked for detecting comma position and counting the arguments
Thank you 🙂
Why not, as a way out there thought, use Base SAS for processing your data?
data _null_; comma=ifc(index("&x.",",")>0,"Y","N"); call symput('commayn',comma); call symput(countw("&x.",","); do i=1 to countw("&x.",","); call symput('newm'||strip(put(i,best.)),scan("&x.",i,",")); end; run;
To be honest its rarely a good idea to have lists of items in macro variables, or be processing these lists in such a way - your code will become overly complex and hard to maintain with no real benefit. Put data in datasets and use Base SAS to manipulate it - macro is for creating generically derived Base SAS code, not for data processing.
@jhh197 wrote:
Hi,
I have a macro &u = 6,22,5
1.) How do I detect from macro u where there is a comma %if scan(&x,',') > 0 will this work ?
2.) How do I count the number of words from the string u ( Answer should be )
3.) How do I extract 6 from string u and 22 from string u and assign that to macro variable and 5 from string u and assign it to macro variable
Please Can anyone help
Thanks
You need to quote the commas in your string so they don't cause trouble.
32 %macro splitstring(arg);
33 %local i w;
34 %do i = 1 %to %sysfunc(countw(%superQ(u),%str(,)));
35 %local w&i;
36 %let w&i = %scan(%superq(u),&i);
37 %end;
38 %let i = %eval(&i-1);
39 %put _local_;
40 %mend;
41 %splitstring(%bquote(&u));
SPLITSTRING ARG 6225
SPLITSTRING I 3
SPLITSTRING W
SPLITSTRING W1 6
SPLITSTRING W2 22
SPLITSTRING W3 5
42 %splitstring((&u));
SPLITSTRING ARG (6,22,5)
SPLITSTRING I 3
SPLITSTRING W
SPLITSTRING W1 6
SPLITSTRING W2 22
SPLITSTRING W3 5
This also helped Thank you so much for quick response
Thank you 🙂
@jhh197 wrote:
This also helped Thank you so much for quick response
Thank you 🙂
It also creates enumerated macro variables with values of each word.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.