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

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" ?

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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.

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

5 REPLIES 5
Astounding
PROC Star

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

 

%let dte = 07072017;

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

danshek
Calcite | Level 5

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.

Astounding
PROC Star

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.

mkeintz
PROC Star

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.

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
danshek
Calcite | Level 5

Thanks mkeintz & Astounding, 

 

Much appreciated. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 3538 views
  • 0 likes
  • 3 in conversation