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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.