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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.