BookmarkSubscribeRSS Feed
jhh197
Pyrite | Level 9

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

12 REPLIES 12
Reeza
Super User

1. It's not clear what you want here

2. COUNTW/COUNT

3. %SCAN or SCAN

 

 

jhh197
Pyrite | Level 9

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

Reeza
Super User

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. 

 

jhh197
Pyrite | Level 9
Yes
I used %let x = index(&u, ',');
% put &x but it's not resolved
Error :the quoted string currently being processed became more than 262 characters long . The string might have unbalanced quotation marks quotation

For count I tried
% let h = %sysfunc(countw(&x,','));
%put &h;

jhh197
Pyrite | Level 9
Sorry I used this macro for count

%let h= %sysfunc(countw(&u,','));
%put &h;
Shmuel
Garnet | Level 18

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;

 

jhh197
Pyrite | Level 9

Hi ,

Thank you this worked for detecting comma position and counting the arguments

 

Thank you 🙂

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

data_null__
Jade | Level 19

@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

 

jhh197
Pyrite | Level 9

This also helped Thank you so much for quick response

 

 

 

Thank you 🙂

data_null__
Jade | Level 19

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 12 replies
  • 1699 views
  • 2 likes
  • 5 in conversation