DATA Step, Macro, Functions and more

Call symput not working as expected

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

Call symput not working as expected

Hi,

 

I have the below call symput statements, when I run for the first time &st_dt. and &en_dt. wont resolve to actual values, but the second run resolves it. Could you please let me know why?

 

First run has the warnings in the log saying that &dte1 and &dte2 not resolved but second run has no warnings.

 

data _null_;
date1=put(intnx('month',today(),-1,'beginning'),yymmdd10.);
call symput('dte1',date1);
call symput ('st_dt',"'&dte1.'");
date2=put(intnx('month',today(),-1,'e'), yymmdd10.);
call symput('dte2',date2);
call symput ('en_dt',"'&dte2.'");


run;
%put &dte1.;
%put &dte2.;
%put &st_dt.;
%put &en_dt.;

 

 

 

1 data _null_;
2 date1=put(intnx('month',today(),-1,'beginning'),yymmdd10.);
3 call symput('dte1',date1);
4 call symput ('st_dt',"'&dte1.'");
WARNING: Apparent symbolic reference DTE1 not resolved.
5 date2=put(intnx('month',today(),-1,'e'), yymmdd10.);
6 call symput('dte2',date2);
7 call symput ('en_dt',"'&dte2.'");
WARNING: Apparent symbolic reference DTE2 not resolved.
8
9
10 run;

NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


11 %put &dte1.;
2016-11-01
12 %put &dte2.;
2016-11-30
13 %put &st_dt.;
'&dte1.'
14 %put &en_dt.;
'&dte2.'
15 data _null_;
16 date1=put(intnx('month',today(),-1,'beginning'),yymmdd10.);
17 call symput('dte1',date1);
18 call symput ('st_dt',"'&dte1.'");
19 date2=put(intnx('month',today(),-1,'e'), yymmdd10.);
20 call symput('dte2',date2);
21 call symput ('en_dt',"'&dte2.'");
22
23
24 run;

NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


25 %put &dte1.;
2016-11-01
26 %put &dte2.;
2016-11-30
27 %put &st_dt.;
'2016-11-01'
28 %put &en_dt.;
'2016-11-30'


Accepted Solutions
Solution
‎12-11-2016 04:36 PM
Super User
Posts: 19,832

Re: Call symput not working as expected

So, as already stated, you cannot use a macro variable in the same data step you created it. If you really want to, use SYMGET or RESOLVE. But since you already have the variables created you can use them as you did in the other CALL SYMPUT.

 

I would recommend, replacing the single quotes with the QUOTE function. I find it's more legible and very clear off the bat what you're trying to do - add quotes, and in this case single quotes. 

 

I would also recommend CALL SYMPUTX as it removes any trailing spaces which is common when creating macro variables.

 

data _null_;
	date1=put(intnx('month', today(), -1, 'beginning'), yymmdd10.);
	date2=put(intnx('month', today(), -1, 'e'), yymmdd10.);
	call symputx('dte1', date1);
	call symputx ('st_dt', quote(date1, "'"));
	call symputx('dte2', date2);
	call symputx ('en_dt', quote(date2, "'"));
run;

%put &dte1.;
%put &dte2.;
%put &st_dt.;
%put &en_dt.;

View solution in original post


All Replies
Super Contributor
Posts: 356

Re: Call symput not working as expected

[ Edited ]

I thought (could be wrong) that the & is executed at complile time and &dte1 and &dte2 don't exist at compile time given it is populated during the datastep

 

The reason it works the second time is that the first data step execution has completed and so the macro vars now exist and can be used when you run again.

 

so change to

 

data _null_;
date1=put(intnx('month',today(),-1,'beginning'),yymmdd10.);
call symput('dte1',date1);
call symput ('st_dt',date1);
date2=put(intnx('month',today(),-1,'e'), yymmdd10.);
call symput('dte2',date2);
call symput ('en_dt', date2);


run;
%put &dte1.;
%put &dte2.;
%put &st_dt.;
%put &en_dt.;

 

 

 

Barry

 

Solution
‎12-11-2016 04:36 PM
Super User
Posts: 19,832

Re: Call symput not working as expected

So, as already stated, you cannot use a macro variable in the same data step you created it. If you really want to, use SYMGET or RESOLVE. But since you already have the variables created you can use them as you did in the other CALL SYMPUT.

 

I would recommend, replacing the single quotes with the QUOTE function. I find it's more legible and very clear off the bat what you're trying to do - add quotes, and in this case single quotes. 

 

I would also recommend CALL SYMPUTX as it removes any trailing spaces which is common when creating macro variables.

 

data _null_;
	date1=put(intnx('month', today(), -1, 'beginning'), yymmdd10.);
	date2=put(intnx('month', today(), -1, 'e'), yymmdd10.);
	call symputx('dte1', date1);
	call symputx ('st_dt', quote(date1, "'"));
	call symputx('dte2', date2);
	call symputx ('en_dt', quote(date2, "'"));
run;

%put &dte1.;
%put &dte2.;
%put &st_dt.;
%put &en_dt.;
Frequent Contributor
Posts: 122

Re: Call symput not working as expected

Thank you all for your valuable feedback!

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 456 views
  • 4 likes
  • 3 in conversation