BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
renjithr
Quartz | Level 8

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'

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

3 REPLIES 3
twocanbazza
Quartz | Level 8

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

 

Reeza
Super User

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.;
renjithr
Quartz | Level 8

Thank you all for your valuable feedback!

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
  • 3 replies
  • 5409 views
  • 4 likes
  • 3 in conversation