Converting Macro Date to yymmn6. format

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Converting Macro Date to yymmn6. format

I have a macro date which I am trying to convert to a yymmn6. format. ie."201707"

 

It seems simple enough.

 

%let dte = 07072017;
 %let dte_9 = %sysfunc(inputn(&dte,ddmmyyyyn8.),yymonn7.);
%put &dte_9;
7072017

 

when I use yymonn7. as the format for the %sysfunc I get "7072017"

 

But when I change the format to yymmn6. I get Jibberish.

 

%let dte = 07072017;
%let dte_9 = %sysfunc(inputn(&dte,ddmmyyyyn8.),yymmn6.);
%put &dte_9;
******

Why is the date outputted as * ? What can I do to get the date of "201707" ?

 


Accepted Solutions
Solution
‎07-05-2017 11:38 PM
Valued Guide
Posts: 797

Re: Converting Macro Date to yymmn6. format

First, your input format (ddmmyyyyn8.) is wrong.  The statement

 

       %let x=%sysfunc(inputn(07072017,ddmmyyyyn8.));

produces the message

     WARNING: Argument 2 to function INPUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.

 

So

  1. correct the informat to DDMMYY8..
  2. correct the output format to YYMMDDN8.
  3. Then take only the first 6 resulting characters

%let dte_9=%substr(%sysfunc(inputn(&dte,ddmmyy8.),yymmddn8.),1,6);

%put &=dte_9;

 

Note that the YYMON format issue an abbreviated month NAME, not month NUMBER.

 

 

 

View solution in original post


All Replies
Super User
Posts: 5,085

Re: Converting Macro Date to yymmn6. format

Have you tried it the easy (but untested) way?

 

%let dte = 07072017;

%let dte_9 = %substr(&dte, 3, 6);

New Contributor
Posts: 3

Re: Converting Macro Date to yymmn6. format

[ Edited ]

I tried

 

%let dte_9 = %substr(&dte,3,6);
%put &dte_9;
072017

 

Which seems to work, but the date format is "072017".

 

How would I then transform it to YYYYMM as "201707"?

 

I tried 

 

%let dte_10 = %sysfunc(inputn(&dte_9,mmyyn6.),yymonn6.);
%put &dte_10;
72017

 

But the leading zero is gone and the date format is mYYYY.

Super User
Posts: 5,085

Re: Converting Macro Date to yymmn6. format

OK, take two pieces of it then:

 

%let dte = 07072017;


%let dte_9 = %substr(&dte, 5, 4)%substr(&dte, 3, 2);

 

Just don't add a space before the second instance of %SUBSTR.

Solution
‎07-05-2017 11:38 PM
Valued Guide
Posts: 797

Re: Converting Macro Date to yymmn6. format

First, your input format (ddmmyyyyn8.) is wrong.  The statement

 

       %let x=%sysfunc(inputn(07072017,ddmmyyyyn8.));

produces the message

     WARNING: Argument 2 to function INPUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.

 

So

  1. correct the informat to DDMMYY8..
  2. correct the output format to YYMMDDN8.
  3. Then take only the first 6 resulting characters

%let dte_9=%substr(%sysfunc(inputn(&dte,ddmmyy8.),yymmddn8.),1,6);

%put &=dte_9;

 

Note that the YYMON format issue an abbreviated month NAME, not month NUMBER.

 

 

 

New Contributor
Posts: 3

Re: Converting Macro Date to yymmn6. format

[ Edited ]

Thanks mkeintz & Astounding, 

 

Much appreciated. 

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 164 views
  • 0 likes
  • 3 in conversation