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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

 

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

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

 

sas_user_null
Calcite | Level 5
That is great and super fast! Thanks a lot!!!
ed_sas_member
Meteorite | Level 14

Hi @sas_user_null 

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;

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
  • 3 replies
  • 1354 views
  • 0 likes
  • 3 in conversation