DATA Step, Macro, Functions and more

SAS Scan

Reply
Contributor
Posts: 37

SAS Scan

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

Super User
Posts: 19,815

Re: SAS Scan

1. It's not clear what you want here

2. COUNTW/COUNT

3. %SCAN or SCAN

 

 

Contributor
Posts: 37

Re: SAS 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

Super User
Posts: 19,815

Re: SAS Scan

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. 

 

Contributor
Posts: 37

Re: SAS Scan

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;

Contributor
Posts: 37

Re: SAS Scan

Sorry I used this macro for count

%let h= %sysfunc(countw(&u,','));
%put &h;
Trusted Advisor
Posts: 1,566

Re: SAS Scan

[ Edited ]

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;

 

Contributor
Posts: 37

Re: SAS Scan

Hi ,

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

 

Thank you :-)

Super User
Super User
Posts: 7,955

Re: SAS Scan

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.

Respected Advisor
Posts: 3,799

Re: SAS Scan

[ Edited ]

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

 

Contributor
Posts: 37

Re: SAS Scan

Posted in reply to data_null__

This also helped Thank you so much for quick response

 

 

 

Thank you :-)

Respected Advisor
Posts: 3,799

Re: SAS Scan


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.

Ask a Question
Discussion stats
  • 12 replies
  • 219 views
  • 2 likes
  • 5 in conversation