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

Hello alltogether,

 

I do have a list of variable names, say: %let test_var=%nrstr(age sex biologica);

Actually this list is much longer.

For each Variable I want to create new variable names like MW_age MW_sex MW_biologica to use them late in my program.

 

I trie to do this with a macro-loop.

But the problem I do have is that only MW_age is known in the end of the macro and  mean_sex and mean_biologica are unknown outside the macro.

 

What do I have to do, to have all macro-variables available in the end?

Regards sasstats

 

Here is my program:

%let test_var=%nrstr(age sex biologica);
%macro test_loop;
	%let i=1;
	%let var=%scan(&test_var.,&i.,' '); 

	%do %While("&var." NE "");
		%put &var.;
		%let MW_var_&i=MW_&var.;
		%put &&MW_Var_&i.;

		%let i=%eval(&i.+1);
		%let var=%scan(&test_var.,&i.,' ');
	%end;
/* here all 3 macro-variables are known */
%put &MW_var_1.;
%put &MW_var_2.;
%put &MW_var_3.;
%mend test_loop;
%test_loop;

/* here only MW_age is known, the others not */ %put &MW_var_1.; %put &MW_var_2.; %put &MW_var_3.;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

The macro variables you create inside the macro TEST_LOOP are only available inside the macro. When you try to use them outside the macro, you can't, and an error results.

 

You can overcome this by putting all the code that uses these variables inside the macro TEST_LOOP; or by using the %GLOBAL command inside of TEST_LOOP, which allows the %GLOBAL macro variables to be used outside of the macro.

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

The macro variables you create inside the macro TEST_LOOP are only available inside the macro. When you try to use them outside the macro, you can't, and an error results.

 

You can overcome this by putting all the code that uses these variables inside the macro TEST_LOOP; or by using the %GLOBAL command inside of TEST_LOOP, which allows the %GLOBAL macro variables to be used outside of the macro.

--
Paige Miller
sasstats
Fluorite | Level 6

Hey,

 

thank you very much for your information!

Using the further syntax inside my macro works fine!

 

sasstats

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 889 views
  • 1 like
  • 2 in conversation