Hi all,
I have a question for using sas variables dynamically. I created a static list prompt. When selected, prompt outputs following variables:
%LET Selected_Scenarios_count = 3;
%LET Selected_Scenarios = Base;
%LET Selected_Scenarios0 = 3;
%LET Selected_Scenarios3 = Down;
%LET Selected_Scenarios1 = Base;
%LET Selected_Scenarios2 = Up;
my variables are working perfectly but I wish to call them dynamically. I wrote the code below but it is not functioning. My question: How can I use the variables within a dynamic loop?
%do i=1 %to &Selected_Scenarios_count.;
Name_&selected_scenarios&i.=value; /*Trying to name the column with respect to variable. eg:'Down'*/
%end;
Thanks.
HI @sas_user_null Assuming your question is why your loop isn't functioning, the reason is you need an extra ampersand & to indirectly reference the macro variable resolution within the loop
So && here should solve the problem
Name_&&selected_scenarios&i.=value;
You can basically test with a %PUT statement to see the generated statements(text) in the log. For example
%LET Selected_Scenarios_count = 3;
%LET Selected_Scenarios = Base;
%LET Selected_Scenarios0 = 3;
%LET Selected_Scenarios3 = Down;
%LET Selected_Scenarios1 = Base;
%LET Selected_Scenarios2 = Up;
%macro t;
%do i=1 %to &Selected_Scenarios_count.;
%put Name_&&selected_scenarios&i.=value; /*Trying to name the column with respect to variable. eg:'Down'*/
%end;
%mend t;
%t
HI @sas_user_null Assuming your question is why your loop isn't functioning, the reason is you need an extra ampersand & to indirectly reference the macro variable resolution within the loop
So && here should solve the problem
Name_&&selected_scenarios&i.=value;
You can basically test with a %PUT statement to see the generated statements(text) in the log. For example
%LET Selected_Scenarios_count = 3;
%LET Selected_Scenarios = Base;
%LET Selected_Scenarios0 = 3;
%LET Selected_Scenarios3 = Down;
%LET Selected_Scenarios1 = Base;
%LET Selected_Scenarios2 = Up;
%macro t;
%do i=1 %to &Selected_Scenarios_count.;
%put Name_&&selected_scenarios&i.=value; /*Trying to name the column with respect to variable. eg:'Down'*/
%end;
%mend t;
%t
In my understanding, you want to create a dataset (let's say dataset 'have') with a number of columns equal to 3 (cf. your macro variable Selected_scenarios_count) and named according to your macrovariables (-> Name_down etc.)
Is that right?
%macro var_name();
data have;
array Name_Selected_Scenarios(&Selected_Scenarios_count.);
%do i=1 %to &Selected_Scenarios_count.;
rename Name_selected_scenarios&i.=Name_&&selected_scenarios&i.;
%end;
run;
%mend;
%var_name;
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.