BookmarkSubscribeRSS Feed
CurtisSmithDCAA
Obsidian | Level 7

I need to read the value of a variable into a macro variable and then in the same data step use the macro variable as the upper limit in a do loop. My code is as follows:

 

data work.test;
set in.test_data;
call symput('mqty',trim(Order_Quantity));
put "Quantity = &mqty.";
do i=1 to "&mqty.";
output;
end;
run;

 

But, the symput only processes the last row in the data set, so it uses that value in each iteration of the do loop.

 

Any suggestions?

7 REPLIES 7
CJac73
Obsidian | Level 7
What @KirkBresmer said. You can still do the macro var, but it is the same value as order_quantity
ScottBass
Rhodochrosite | Level 12

I need to read the value of a variable into a macro variable and then in the same data step use the macro variable as the upper limit in a do loop.

 

This will never work.  Read the macro doc to understand the difference between compile time vs. run time execution.

 

In your example:

 

data work.test;
set in.test_data;
call symput('mqty',trim(Order_Quantity));
put "Quantity = &mqty.";
do i=1 to "&mqty.";
output;
end;
run;

&mqty (lines 4 & 5) is evaluated before the data step starts execution.

 

Also, I recommend call symputx over call symput.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
Tom
Super User Tom
Super User

There is no need in your example since you already have the value in the dataset.

If you did need to read the value of a macro variable dynamically then use SYMGET() or SYMGETN() function.

Try this program.

 

%let x1=5;
%let x2=3;
data test;
do row=1 to 2;
  do obs=1 to symgetn(cats('x',row));
    output;
  end;
end;
run;
Astounding
PROC Star
do i=1 to symgetn('mqty');
ScottBass
Rhodochrosite | Level 12

@Tom and @Astounding are very smart SAS professionals, and I bow to their expertise.  They've both helped me more times than I can count (although that presumes I can count very high).

 

However, in this instance, I'll point out that I feel their examples are illustrative only, just so you understand the concept of symget and symgetn.  I don't think using symget is the approach you should be using in your example.  (Actually, Tom made it clear that it was just an example).

 

If you find yourself loading a data step variable into a macro variable (call symput), then wanting to reference that macro variable in the same data step (symget), 99.99% of the time you should just use the data step variable instead.  Moving data to/from the macro symbol table is much slower than using data in the PDV/data step.

 

In summary, use @Kurt_Bremser 's approach.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
Kurt_Bremser
Super User

And if you need to keep values across observations, the data step provides multiple tools for this:

  • lag() functions
  • retained variables
  • temporary arrays
  • hash objects

which will all perform better than invoking the macro processor repeatedly while the step is running.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1127 views
  • 3 likes
  • 6 in conversation