DATA Step, Macro, Functions and more

Informat for date in the form MM.YYYY

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Informat for date in the form MM.YYYY

Hi,

 

What informat shall I use to input a date in the form MM.YYYY (period as separator). Example: March 2016 is represented as 03.2016.

 

ANYDTDTE7. works when hyphen (-) or slash (/) are used as separators. Returns a missing value when period is used as a separtor.

 

Thanks.


Accepted Solutions
Solution
‎01-23-2017 02:13 AM
PROC Star
Posts: 1,564

Re: Informat for date in the form MM.YYYY

[ Edited ]

You can read an external file directly if you redefine the value to read on the fly, and add the missing day :

 

proc format;
  invalue mmyyp (default=10)
          's/(.{7})/01.\1/o' (regexpe)=[ddmmyy10.];
run;

data WANT;
  input DATE mmyyp.;
  putlog DATE= date9. ;
cards;
03.2016
run;

 DATE=01MAR2016

 

More examples about all valid SAS regular expressions, including their use in informats, by following this link:

 

View solution in original post


All Replies
Super User
Posts: 5,085

Re: Informat for date in the form MM.YYYY

Dates in SAS refer to a particular day.  If you have just the month and year (in any format), you first need to decide what day of the month to use in order to convert your data to a SAS date.

Occasional Contributor
Posts: 9

Re: Informat for date in the form MM.YYYY

Hi,

Thanks for your reply. 

'Day' does not really matter; it can be taken as 01.

 

I just need to read a file which will have month and year for a particular event.

As I mentioned, ANYDTDTE7. works fine with hyphen or slash as separators, that is, the following code gives expected output as 10.2016

 

 

data _null_;
informat dte anydtdte7.;
format dte mmyyp7.;
input dte;
put dte;
datalines;

10/2016
;
run; 

 

Taking 10-2016 in place of 10/2016 also works; but unfortunately, my input will be as 10.2016, which gives a missing value as output. 

Super User
Posts: 5,085

Re: Informat for date in the form MM.YYYY

In that case, you might have to work around the problem by reading as text then changing "." to "/":

 

input textvar $;

dte = input( translate(textvar, '/', '.'), anydtdte7.);

Trusted Advisor
Posts: 1,392

Re: Informat for date in the form MM.YYYY

Just for the fun you may even do (code tested and is ok):

 

data test;

       input date best7.4;

       date = input('01'||put(date*10000,z6.),ddmmyy8.);

      format date ddmmyy10.;

datalines;

03.2017

11.2016

5.1999

; run;

Solution
‎01-23-2017 02:13 AM
PROC Star
Posts: 1,564

Re: Informat for date in the form MM.YYYY

[ Edited ]

You can read an external file directly if you redefine the value to read on the fly, and add the missing day :

 

proc format;
  invalue mmyyp (default=10)
          's/(.{7})/01.\1/o' (regexpe)=[ddmmyy10.];
run;

data WANT;
  input DATE mmyyp.;
  putlog DATE= date9. ;
cards;
03.2016
run;

 DATE=01MAR2016

 

More examples about all valid SAS regular expressions, including their use in informats, by following this link:

 

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 217 views
  • 2 likes
  • 4 in conversation