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.;
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.
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.
Hey,
thank you very much for your information!
Using the further syntax inside my macro works fine!
sasstats
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.