DATA Step, Macro, Functions and more

how can I automatically count the number of items in a macro variable?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

how can I automatically count the number of items in a macro variable?

Hi,

I would like to know how to automatically determine the number of items in & type(=26) instead of declaring 26 in the below script....
Just a very simple example:

 

%let type= A B C D E F G H I J K L M N O P R S T U V W X Y Z ;

data _NULL_;

do i = 1 to 26;   <---- this value of 25 is what I would like to automate when I change &type.

put Number=i ;

end;

run;

 


Accepted Solutions
Solution
‎02-23-2018 10:33 AM
Valued Guide
Posts: 561

Re: how can I automatically count the number of items in a macro variable?

Posted in reply to Jiawenabby8

Assuming they're always space separated then the countw function should do the trick like so

 

%let type= A B C D E F G H I J K L M N O P R S T U V W X Y Z;
data _NULL_;
do i = 1 to countw("&type");  
put Number=i ;
end;
run;

View solution in original post


All Replies
Solution
‎02-23-2018 10:33 AM
Valued Guide
Posts: 561

Re: how can I automatically count the number of items in a macro variable?

Posted in reply to Jiawenabby8

Assuming they're always space separated then the countw function should do the trick like so

 

%let type= A B C D E F G H I J K L M N O P R S T U V W X Y Z;
data _NULL_;
do i = 1 to countw("&type");  
put Number=i ;
end;
run;
Super User
Posts: 22,874

Re: how can I automatically count the number of items in a macro variable?

Posted in reply to Jiawenabby8
Occasional Contributor
Posts: 5

Re: how can I automatically count the number of items in a macro variable?

Your links are super helpful. Thanks so much!

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 91 views
  • 4 likes
  • 3 in conversation