Hello all. I have the following macro:
%macro enum (x,string);
%put &x. &string.;
%mend enum;
%enum(1,%str(horg));
%enum(2,%str(blorg));
%enum(3,%str(chorg));
It returns:
1 horg
2 blorg
3 chorg
However, I need to run this for many lines and I don't want to type each numeral (4, 5, 6, etc). How can I run this without specifying the numeral in each line? Can the macro count how many times it has been run in a SAS session? Or do I need another macro?
Thank you!
Make X a global macro variable instead of a parameter. If you want to reset the counter just assign a value to the global macro variable.
%macro enum (string);
%global enum_count ;
%let enum_count=%eval(&enum_count+1);
%put &enum_count. &string.;
%mend enum;
31 %let enum_count=0; 32 %enum(horg); 1 horg 33 %enum(blorg); 2 blorg 34 %enum(chorg); 3 chorg
Is this what you want? (If not, describe further)
%macro enum (string);
%do i=1 %to %sysfunc(countw(&string));
%let text=%scan(&string,&i,%str( ));
%put &i &text;
%end;
%mend enum;
%enum(abc def ghi jkl)
Adding: you can do this in a data step as well, so unless you have a good reason for doing it in a macro, do it in a data step.
If the macro calls have to be separate, then I don't understand the desired functioning here. Please explain further. And also, explain why it has to be a macro and not a data step.
Make X a global macro variable instead of a parameter. If you want to reset the counter just assign a value to the global macro variable.
%macro enum (string);
%global enum_count ;
%let enum_count=%eval(&enum_count+1);
%put &enum_count. &string.;
%mend enum;
31 %let enum_count=0; 32 %enum(horg); 1 horg 33 %enum(blorg); 2 blorg 34 %enum(chorg); 3 chorg
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!
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.