Hi all, I have the next error message in this code:
WARNING: Apparent symbolic reference RECORD1 not resolved.
WARNING: Apparent symbolic reference RECORD2 not resolved.
WARNING: Apparent symbolic reference RECORD3 not resolved.
WARNING: Apparent symbolic reference RECORD4 not resolved.
Can you tell me what I am doing wrong? Thanks. V
****piece of code;
data have;
input records;
datalines;
1
2
3
4
;
run;
%macro test(n=);
data want&n.;
set have (obs=&n.);
if n=1 then call symput("record1",records);
if n=2 then call symput("record2",records);
if n=3 then call symput("record3",records);
if n=4 then call symput("record4",records);
run;
%mend test;
%test(n=1);
%test(n=2);
%test(n=3);
%test(n=4);
%put &record1 &record2 &record3 &record4;
You create the macro variables within a macro and so they have a scope of local.
You need to add the following inside your macro:
%global &record1 &record2 &record3 &record4;
I would also add a %let statement to initialise the macro vars so that you don't have "left over" populated macro vars from previous runs.
If I understand correctly what you're trying to do then you should also use "&n" instead of "n" in your if condition.
if &n=1 then call symput("record1",records);
...and as only one of the conditions will be true you should also use an else:
if &n=1 then call symput("record1",records);
else if &n=2 then call symput("record2",records);
.....
Sorry, my mistake....but still does not work...any help? Thanks.
if &n.=1 then call symput("record1",records);
if &n.=2 then call symput("record2",records);
if &n.=3 then call symput("record3",records);
if &n.=4 then call symput("record4",records);
data have;
input records;
datalines;
1
2
3
4
;
run;
options symbolgen mprint mlogic;
%macro test(n=);
%global record&n.;
data want&n.;
set have (firstobs=&n. obs=&n.);
call symputx("record"||left(put(&n.,2.)),records);
run;
%mend test;
%test(n=1);
%test(n=2);
%test(n=3);
%test(n=4);
%put &record1 &record2 &record3 &record4 ;
Thanks,
Jagadish
Answer already provided in my initial post. Read it all.
Thanks guys for your reply,
Dear Patrick, several questions:
What is the difference of using a) or b),
this b) option could work in my code also?
a)if &n=1 then call symput("record1",records);
b)%if &n=1 %then call symput("record1",records);
Thanks in advance,
V.
In the SAS Macro doc and I believe also in the SAS Concepts doc is a detailed description of how the SAS Macro and SAS Language processors interact. It's very important to understand this.
As for your question:
Lets assume "&n" has a value of "2":
a) resolves on macro level "&n". The SAS language sees then: if 2=1 then call symput("record1",records);
b) tests the macro %if condition on macro level. As the condition is not true the SAS language "sees" nothing. If the condition would be true the SAS language would see: call symput("record1",records);
An alternative way of coding only creating the macro variable for the current value of "&n":
%macro test(n=);
%global record&n;
data want&n.;
set have (obs=&n.);
call symputx("record&n",records);
run;
%mend test;
You create the macro variables within a macro and so they have a scope of local.
You need to add the following inside your macro:
%global &record1 &record2 &record3 &record4;
I would also add a %let statement to initialise the macro vars so that you don't have "left over" populated macro vars from previous runs.
If I understand correctly what you're trying to do then you should also use "&n" instead of "n" in your if condition.
if &n=1 then call symput("record1",records);
...and as only one of the conditions will be true you should also use an else:
if &n=1 then call symput("record1",records);
else if &n=2 then call symput("record2",records);
.....
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.