SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello
Why the result in the following example giving a null result .
I expect to get the value of 15JAN2019 (because today is 15APR2019)

 

 

data _null_;
call symput('x1',intnx('month',today(),-3,s)); /* 01JAN2019*/
RUN;
%put &x1;
 
4 REPLIES 4
novinosrin
Tourmaline | Level 20

enclose s in quotes

 

's'

 

data _null_;
1343 call symput('x1',put(intnx('month',today(),-3,'s'),date9.)); /* 01JAN2019*/
1344 RUN;

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


SYMBOLGEN: Macro variable X1 resolves to 15JAN2019
1345 %put &x1;
15JAN2019

 

novinosrin
Tourmaline | Level 20

Or if you want the date to revert to 1st of the month, remove 's'

 

Like

 

data _null_;
call symput('x1',put(intnx('month',today(),-3),date9.)); /* 01JAN2019*/
RUN;
%put &x1;

6 data _null_;
1347 call symput('x1',put(intnx('month',today(),-3),date9.)); /* 01JAN2019*/
1348 RUN;

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


SYMBOLGEN: Macro variable X1 resolves to 01JAN2019
1349 %put &x1;
01JAN2019

Astounding
PROC Star

If you were writing the code using macro language functions, you would get the correct result.

 

In a DATA step, however, 

 

"s"  =  "same"

 

s = use the value of the DATA step variable named S to indicate whether you want the beginning, same, or ending date

 

Add quotes around the S, and all will be well.

Tom
Super User Tom
Super User

@Ronein wrote:

Hello
Why the result in the following example giving a null result .
I expect to get the value of 15JAN2019 (because today is 15APR2019)

 

 

data _null_;
call symput('x1',intnx('month',today(),-3,s)); /* 01JAN2019*/
RUN;
%put &x1;
 

For all of the reasons that SAS states in the LOG.

1226  data _null_;
1227  call symput('x1',intnx('month',today(),-3,s)); /* 01JAN2019*/
1228  RUN;

NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
      1227:18   1227:43
NOTE: Variable s is uninitialized.
NOTE: Argument 4 to function INTNX('month',21654,-3,'           .') at line 1227 column 18 is invalid.
s=. _ERROR_=1 _N_=1
NOTE: Mathematical operations could not be performed at the following places. The results of the
      operations have been set to missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 1227:18
NOTE: DATA statement used (Total process time):
      real time           0.24 seconds
      cpu time            0.03 seconds


1229  %put &x1;
.
1230  %put |&x1|;
|           .|

You did not create the variable S that you used in your INTNX() function call so SAS created it for you as a numeric varaible. Since that position in the function call needs a string SAS converted the missing value of S to a string using the BEST12. format.  So then INTNX() function complained when the value of the last parameter was eleven spaces followed by a period, as that was not one of the supported values.  So INTNX() returned missing.  Now the second argument to CALL SYMPUT needs to be a string so SAS again had to convert that missing numeric value into a string to write into the macro variable X1.  If you look closely you will see that X1 actually has the same eleven spaces plus a period that INTNX() complained about because that is what BEST12 format produces for missing values.

 

If you add quotes around the letter S then SAS will know you mean the string with a value of S instead of a variable named S.

If you use CALL SYMPUTX() then it will convert the number to a string automatically for you without the note.

If you add a call to the PUT() (or PUTN() ) function then you can set the macro variable to the FORMATTED value.

1231  data _null_;
1232  call symputX('x1',put(intnx('month',today(),-3,'s'),date9.));
1233  RUN;

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


1234  %put &x1;
15JAN2019
1235  %put |&x1|;
|15JAN2019|

Or you could skip the data step do the whole thing in macro logic.  In that case you don't need use quotes since in macro code you use & to let it know when you are referencing a macro variable so quotes are not needed to tell the difference between strings and names. Everything is string to the macro processor.

1236  %let x1=%sysfunc(intnx(month,%sysfunc(today()),-3,s),date9.);
1237  %put &x1;
15JAN2019

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1233 views
  • 4 likes
  • 4 in conversation