Use && in data step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Use && in data step

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.


Accepted Solutions
Solution
‎04-03-2018 04:50 PM
Respected Advisor
Posts: 2,989

Re: Use && in data step

[ Edited ]
Posted in reply to liangchh0
%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


All Replies
Super User
Posts: 23,683

Re: Use && in data step

Posted in reply to liangchh0

SYMGET() function

Occasional Contributor
Posts: 9

Re: Use && in data step

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.
Solution
‎04-03-2018 04:50 PM
Respected Advisor
Posts: 2,989

Re: Use && in data step

[ Edited ]
Posted in reply to liangchh0
%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
Occasional Contributor
Posts: 9

Re: Use && in data step

Posted in reply to PaigeMiller
Thanks for your reply. The code doesn't seem to work. Is there a typo or any further modification required?
Super User
Posts: 13,508

Re: Use && in data step

Posted in reply to liangchh0

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.

Respected Advisor
Posts: 2,989

Re: Use && in data step

Posted in reply to liangchh0

@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
Occasional Contributor
Posts: 9

Re: Use && in data step

Posted in reply to PaigeMiller

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?

Super User
Posts: 10,211

Re: Use && in data step

[ Edited ]
Posted in reply to liangchh0

@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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 9

Re: Use && in data step

Posted in reply to KurtBremser

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.

Super User
Posts: 10,211

Re: Use && in data step

Posted in reply to liangchh0

Almost there. See:

data dsTest;
    array varArray (2) var1-var2;
    do i=1 to 2;
        vararray(i) = i;
    end;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 9

Re: Use && in data step

Posted in reply to KurtBremser
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.
Valued Guide
Posts: 590

Re: Use && in data step

[ Edited ]
Posted in reply to liangchh0

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
Super User
Posts: 10,211

Re: Use && in data step

Posted in reply to SuryaKiran

That won't work either, because &&var&count will be evaluated before the data step runs and executes the call symput.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Valued Guide
Posts: 590

Re: Use && in data step

Posted in reply to KurtBremser

@KurtBremser 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
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 14 replies
  • 249 views
  • 3 likes
  • 6 in conversation