DATA Step, Macro, Functions and more

How to format a macro variable to mmddyy10.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

How to format a macro variable to mmddyy10.

Hello,  I am trying to format the value of &str (which resolves to 21639) below. I have tried various inputc, putc, inputn, putn variations  but can't seem to come up with a macro variable with the value of 12/31/2019.  

 

code below ****************************

%let str=intnx('quarter',intnx('quarter', '01JAN2017'd, 5, "sameday"), 3, "end");

data _null_;
call symputx('dt', &str,'G');
run;

%put dt=&dt;

 

* a couple attempts of formatting the value;

data _null_;
call symputx('dt1', %sysfunc(inputn(&str, 6.), mmddyy10.),'G');
call symputx('dt2', %sysfunc(21639,mmddyy10.),'G'));

run;

%put dt1=&dt1;
%put dt2=&dt2;

 

log **************************************************

17 %let str=intnx('quarter',intnx('quarter', '01JAN2017'd, 5, "sameday"), 3, "end");
18
19 data _null_;
20 call symputx('dt', &str,'G');
21 run;

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

22
23 %put dt=&dt;
dt=21639
24
25 data _null_;
26 call symputx('dt1', %sysfunc(inputn(&str, 6.), mmddyy10.),'G');
WARNING: Argument 1 to function INPUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The result of the operations have been set
to a missing value.
27 call symputx('dt2', %sysfunc(21639,mmddyy10.),'G'));
ERROR: Function name missing in %SYSFUNC or %QSYSFUNC macro function reference.
27 call symputx('dt2', %sysfunc(21639,mmddyy10.),'G'));
_
159
ERROR 159-185: Null parameters for SYMPUTX are invalid.

27 call symputx('dt2', %sysfunc(21639,mmddyy10.),'G'));
_________
386
ERROR 386-185: Expecting an arithmetic expression.

27 call symputx('dt2', %sysfunc(21639,mmddyy10.),'G'));
_________
201
2 The SAS System 10:40 Wednesday, June 27, 2018

ERROR 201-322: The option is not recognized and will be ignored.

27 call symputx('dt2', %sysfunc(21639,mmddyy10.),'G'));
_________
76
ERROR 76-322: Syntax error, statement will be ignored.

28
29 run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

30
31 %put dt1=&dt1;
dt1=.
32 %put dt2=&dt2;
dt2=0.0000479317
33
34


Accepted Solutions
Solution
3 weeks ago
Occasional Contributor
Posts: 15

Re: How to format a macro variable to mmddyy10.

Posted in reply to novinosrin

You are right - the statement was more complicated than it needed to be.  I did shorten it and tried it out.  Why do I get different results when I assign the value to the macro variable using a symput vs %let?

 

%let str=%sysfunc(intnx(quarter, '01JAN2017'd, 8, end),mmddyy10.);

data _null_;
call symput('dt1',&str);
run;
%put &dt1;

%let dt2=&str;
%put &dt2;

 

log ***************

17 %let str=%sysfunc(intnx(quarter, '01JAN2017'd, 8, end),mmddyy10.);
18
19 data _null_;
20 call symput('dt1',&str);
21 run;

NOTE: Numeric values have been converted to character values at the places given by: (Line)Smiley SadColumn).
20:6
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

22 %put &dt1;
0.0000479317
23
24 %let dt2=&str;
25 %put &dt2;
03/31/2019

View solution in original post


All Replies
PROC Star
Posts: 1,803

Re: How to format a macro variable to mmddyy10.

Posted in reply to Julie4435637
%let str=%sysfunc(intnx(quarter,%sysfunc(intnx(quarter, '01JAN2017'd, 5, sameday)), 3, end),mmddyy10.);

%put &str;
PROC Star
Posts: 1,803

Re: How to format a macro variable to mmddyy10.

Posted in reply to novinosrin

Also just wondering the purpose of such long expression?

Coz the below two produces the same result-->

/*lenghty version*/
%let str=%sysfunc(intnx(quarter,%sysfunc(intnx(quarter, '01JAN2017'd, 5, sameday)), 3, end),mmddyy10.);

%put &str;
/*short version*/
%let k=%sysfunc(intnx(quarter, '01JAN2017'd, 8, end),mmddyy10.);
%put &k;

Log:

 

753 /*lenghty version*/
754 %let str=%sysfunc(intnx(quarter,%sysfunc(intnx(quarter, '01JAN2017'd,
754! 5, sameday)), 3, end),mmddyy10.);
755
756 %put &str;
03/31/2019
757 /*short version*/
758 %let k=%sysfunc(intnx(quarter, '01JAN2017'd, 8, end),mmddyy10.);
759 %put &k;
03/31/2019

Solution
3 weeks ago
Occasional Contributor
Posts: 15

Re: How to format a macro variable to mmddyy10.

Posted in reply to novinosrin

You are right - the statement was more complicated than it needed to be.  I did shorten it and tried it out.  Why do I get different results when I assign the value to the macro variable using a symput vs %let?

 

%let str=%sysfunc(intnx(quarter, '01JAN2017'd, 8, end),mmddyy10.);

data _null_;
call symput('dt1',&str);
run;
%put &dt1;

%let dt2=&str;
%put &dt2;

 

log ***************

17 %let str=%sysfunc(intnx(quarter, '01JAN2017'd, 8, end),mmddyy10.);
18
19 data _null_;
20 call symput('dt1',&str);
21 run;

NOTE: Numeric values have been converted to character values at the places given by: (Line)Smiley SadColumn).
20:6
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

22 %put &dt1;
0.0000479317
23
24 %let dt2=&str;
25 %put &dt2;
03/31/2019

PROC Star
Posts: 1,803

Re: How to format a macro variable to mmddyy10.

Posted in reply to Julie4435637

because you missed the double quotes in the call symput statement

 

%let str=%sysfunc(intnx(quarter, '01JAN2017'd, 8, end),mmddyy10.);

data _null_;
call symput('dt1',"&str");
run;
%put &dt1;

 

Log:

 

786 %let str=%sysfunc(intnx(quarter, '01JAN2017'd, 8, end),mmddyy10.);
787
788 data _null_;
789 call symput('dt1',"&str");
790 run;

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


791 %put &dt1;
03/31/2019

 

Occasional Contributor
Posts: 15

Re: How to format a macro variable to mmddyy10.

Posted in reply to Julie4435637
Thank you.
Super User
Posts: 13,542

Re: How to format a macro variable to mmddyy10.

Posted in reply to Julie4435637

Is there an actual reason to use the double intnx instead of

intnx('quarter', '01JAN2017'd, 8, "end");

 

If you are using some other parameters it might be better to just add them instead of that double intnx.

It might be needed if you were using different intervals but calendar quarter is a calendar quarter and end will be the same.

New Contributor
Posts: 2

Re: How to format a macro variable to mmddyy10.

Posted in reply to Julie4435637

Julie, a value of 21639 converts to day 03/31/2019. It appears that your "%let str" statement might need a value of 6 in the outer intnx call which will provide a value of 21914 and will resolve to 12/31/2019.

 

My code isn't pretty, but I think it illustrates the concept adequately.

 

options symbolgen;

%let str1=intnx('quarter',intnx('quarter', '01JAN2017'd, 5, "sameday"), 3, "end");

%let str2=intnx('quarter',intnx('quarter', '01JAN2017'd, 5, "sameday"), 6, "end");

 

data _null_;

call symputx('dt1', &str1,'G');

call symputx('dt2', &str2,'G');

run;

 

data temp;

format mydt1 mydt2 mmddyy10.;

mydt1=&dt1;

mydt2=&dt2;

run;

 

%put _all_;

 

proc print;run;

 

 

Obs         mydt1            mydt2
103/31/201912/31/2019
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 69 views
  • 0 likes
  • 4 in conversation