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

Hi all,

 

I'd like to if it is possible to call macro variables by do loop in data step and how to achieve that.

Specifically, how to make the following code work?

 

%let var1 = varName1;

%let var2 = varName2;

data dsTest;

    do i=1 to 2;

        &&var&i = i;

    end;

run;

 

Thanks for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
%let var1 = varName1;
%let var2 = varName2;
data dsTest;
    %do i=1 %to 2;
        &&var&i = &i;
    %end;
run;

Only works inside a macro (can't have a %do loop in open code)

--
Paige Miller

View solution in original post

14 REPLIES 14
Reeza
Super User

SYMGET() function

liangchh0
Fluorite | Level 6
Thanks for your reply. As far as I know, symget() is usually applied to the right-hand side of "=". I've tried
data dsTest;
do i=1 to 2;
symget(cats('var',i)) = i;
end;
run;

but it didn't work.
Could you show me explicitly how to make symget() work in this situation?
Thank you so much.
PaigeMiller
Diamond | Level 26
%let var1 = varName1;
%let var2 = varName2;
data dsTest;
    %do i=1 %to 2;
        &&var&i = &i;
    %end;
run;

Only works inside a macro (can't have a %do loop in open code)

--
Paige Miller
liangchh0
Fluorite | Level 6
Thanks for your reply. The code doesn't seem to work. Is there a typo or any further modification required?
ballardw
Super User

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

PaigeMiller
Diamond | Level 26

@liangchh0 wrote:
Thanks for your reply. The code doesn't seem to work. Is there a typo or any further modification required?

Utterly useless to say it "doesn't seem to work".

 

Tell us what happened. Show us the SASLOG, with

options mprint; 

before your code.

--
Paige Miller
liangchh0
Fluorite | Level 6

The code does work properly after I restart SAS.

Sorry for the confusion.

I have no idea why it didn't work before.

Somehow SAS acts weird after I canceling submissions.

Here are the error messages I got when that happens.

 

1732 %MACRO test();
1733 data dsTest;
1734 %let var1 = varName1;
1735 %let var2 = varName2;
1736 %do i=1 %to 2;
1737 &&var&i = &i;
1738 %end;
1739 run;
1740 %MEND test;
1741 %test;
MPRINT(TEST): data dsTest;
WARNING: Apparent symbolic reference VAR not resolved.
NOTE: Line generated by the invoked macro "TEST".
3 &&var&i = &i;
-
180
MPRINT(TEST): &&var1 = 1;
WARNING: Apparent symbolic reference VAR not resolved.
NOTE: Line generated by the invoked macro "TEST".
5 &&var&i = &i;
-
180
MPRINT(TEST): &&var2 = 2;
MPRINT(TEST): run;

ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.DSTEST may be incomplete. When this step was stopped there were 0
observations and 0 variables.
WARNING: Data set WORK.DSTEST was not replaced because this step was stopped.

 

I guess SAS is trying to finish the canceled statements and mixing them with the current statements.

Do you know how to tell SAS to "forget" the unfinished execution?

Kurt_Bremser
Super User

@liangchh0 wrote:

Hi all,

 

I'd like to if it is possible to call macro variables by do loop in data step and how to achieve that.

Specifically, how to make the following code work?

 

%let var1 = varName1;

%let var2 = varName2;

data dsTest;

    do i=1 to 2;

        &&var&i = i;

    end;

run;

 

Thanks for your help.


Since the macro triggers are evaluated BEFORE the data step is compiled and runs, data step variable i cannot be used to indirectly address macro variables. If you need dynamic access to data step variables, use array processing.

liangchh0
Fluorite | Level 6

Hi KurtBremser,

 

Thanks for your reply.

I've tried the following modification but it still doesn't work.

 

data dsTest;

    array varArray (2) var1-var2;

    do i=1 to 2;

        &array(i) = i;

    end;

run;

 

Could you please show me an example?

Thank you so much.

liangchh0
Fluorite | Level 6
Thanks for your reply. What I actually need is to call macro variable var1 and var2 such that

var1=1 becomes varName1=1

Is there a way to achieve that?
Thank you.
SuryaKiran
Meteorite | Level 14

Try something like this:

%let var1 = varName1;
%let var2 = varName2;

data dsTest;
%let Count=1;
count=1;
    do i=1 to 2;

        &&var&Count = count;
		count+1;
Call symput("Count",count); end; run;
Thanks,
Suryakiran
SuryaKiran
Meteorite | Level 14

@Kurt_Bremser Your right, I'm totally wrong about using CALL SYMPUT there before the Data step is executed.

 

@liangchh0 Did you try the solution @PaigeMiller mentioned, You need to not that it has to be enclosed in a macro.

 

%let var1 = varName1;
%let var2 = varName2;
OPTIONS symbolgen mprint mlogic;
%MACRO test();
data dsTest(drop=i);
count=1;
    %do i=1 %to 2;
        &&var&i = &i;
%end;

run;
%MEND test;
%test;
Thanks,
Suryakiran

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 14 replies
  • 3767 views
  • 3 likes
  • 6 in conversation