BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kpberger
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
kpberger
Obsidian | Level 7
Thanks for your reply. The macro calls do have to remain separate, and yes it has to be in a macro. Do you know of a way where the macro calls can remain separate?
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Reeza
Super User
Look up CALL EXECUTE().
Tom
Super User Tom
Super User

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
kpberger
Obsidian | Level 7
Perfect, thank you so much!!
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1618 views
  • 1 like
  • 4 in conversation