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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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

5 REPLIES 5
Astounding
PROC Star

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.

ShagilImam
Obsidian | Level 7

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. 

Astounding
PROC Star

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.);

Shmuel
Garnet | Level 18

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;

ChrisNZ
Tourmaline | Level 20

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:

 

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
  • 3098 views
  • 2 likes
  • 4 in conversation