BookmarkSubscribeRSS Feed
Lakes74
Calcite | Level 5
Hello,

I’m trying to call a macro within a data step and only seems to be evaluating on the second run of the my program!

For example, if i run the following code below, N, T1, T2 and T3 will be null even though the log shows the macro has run and set each global variable.

Only when the program is run a second time (within the SAS session), dataset TEST is written too.

I’m desperate to find a solution to this if possible? Why does it only compute on the second run of the SAS program.

Thank you in advance. Lakes74.



Data look_up;

input id D1 $ D2 $ D3 $;

datalines;

1 A B C

2 D E F

3 G H I

4 J K L

5 M N O

6 P Q R

7 S T U

8 V W X

9 Y Z A

;

run;



%global var1;

%global var2;

%global var3;



%macro temp(number);

proc sql noprint;

select D1,D2,D3 into :var1, :var2, :var3

from look_up

where id = &number;

quit;

%mend;



%let id = 2;



data test;

call execute ('%temp('||put(&id,3.)||')');

N = "&id";

T1 = "&var1";

T2 = "&var2";

T3 = "&var3";

run;



%put &var1;

%put &var2;

%put &var3;
4 REPLIES 4
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
You are attempting to mix SAS MACRO language, macro variable usage, and SAS DATA step processing, which normally is not going to work properly. Read up on CALL EXECUTE and you'll see that the invoked code will actually execute *AFTER* the current DATA step completes.

Suggest adding the code below to get the most output in your SAS log with your compiled and executed code:

OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN MPRINT MLOGIC;

If you have a particular need to use SAS macro language facilities, it's best to get your code working without macros, understand the flow such that it works properly, and then decide what's needed with macros, if at all, to enhance or make your program more effective/efficient by using SAS macros and macro variables.

Scott Barry
SBBWorks, Inc.

Recommended Google advanced search arguments, this topic/post:

macro language introduction site:sas.com

macro variable scoping site:sas.com
DanielSantos
Barite | Level 11
Agree with Scott.

Mixing macro variables with dataset variables could be quite challenging.

From your example you are trying to read a dataset from within a dataset.

This could be done with some other techniques, like index match, hashing or even using the SAS File I/O functions.

Check the online documentation and examples:
http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000148395.htm

Cheers from Portugal.

Daniel Santos @ www.cgd.pt
ChrisNZ
Tourmaline | Level 20
You can't run proc sql inside a data step, think about it.

2 ways to do what you want:

Either look up the data some other way (no need to call a macro to look up data), or if you need to do more complex things in your macro, it has to be pure macro language for the data step to run it immediately.

This does both:
[pre]

data look_up(index=(ID));
input id D1 $ D2 $ D3 $;
datalines;
1 A B C
2 D E F
3 G H I
4 J K L
5 M N O
6 P Q R
7 S T U
8 V W X
9 Y Z A
run;


%macro temp(number);
%global var1 var2 var3;
%let dsid=%sysfunc(open(look_up(where=(ID = &number))));
%let rc=%sysfunc(fetch(&dsid));
%let var1=%sysfunc(getvarc(&dsid,%sysfunc(varnum(&dsid,D1))));
%let var2=%sysfunc(getvarc(&dsid,%sysfunc(varnum(&dsid,D2))));
%let var3=%sysfunc(getvarc(&dsid,%sysfunc(varnum(&dsid,D3))));
%let rc=%sysfunc(close(&dsid));
%mend;


%let id = 2;

data TEST;
call execute ('%temp('||put(&id,3.)||')'); *get values from macro;
T1 = symget('var1');
T2 = symget('var2');
T3 = symget('var3');
output;
ID = &id+1;
set look_up key=ID; *get values from look-up key
T1 = D1;
T2 = D2;
T3 = D3;
output;
stop;
run;[/pre]
Lakes74
Calcite | Level 5
Thanks all for the helpful replies, really appreciate it!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 1341 views
  • 0 likes
  • 4 in conversation