DATA Step, Macro, Functions and more

Date informat

Reply
Trusted Advisor
Posts: 1,204

Date informat

Hi Everyone,

 

I've date variable containing values in this format

 

1-July-2015

2-July-2015

 

What informat should I use to get that variable into SAS data set. Please advise.

 

Regards,

 

 

Super User
Posts: 17,840

Re: Date informat

ANYDTDTE.

Trusted Advisor
Posts: 1,204

Re: Date informat

Thanks Reeza. I tried but its not working. Please see below the data which I am trying to bring in.

 

data have;
input Product $ Qty Date ANYDTDTE. Status $;
datalines;
A 150 1-June-2015 C
A 120 1-July-2015 P
B 900 1-July-2015 C
B 80   1-Aug-2015 P
C 200 1-Aug-2015 P
D 200 1-Aug-2015 C
;

Super User
Posts: 17,840

Re: Date informat

I don't see an INFORMAT statement.

 


stat_sas wrote:

Thanks Reeza. I tried but its not working. Please see below the data which I am trying to bring in.

 

data have;
input Product $ Qty Date ANYDTDTE. Status $;
datalines;
A 150 1-June-2015 C
A 120 1-July-2015 P
B 900 1-July-2015 C
B 80   1-Aug-2015 P
C 200 1-Aug-2015 P
D 200 1-Aug-2015 C
;


 

Super User
Posts: 10,508

Re: Date informat

Please see:

data have;
informat Date ANYDTDTE.; 
input Product $ Qty Date  Status $;
format date mmddyy10.;
datalines;
A 150 1-June-2015 C
A 120 1-July-2015 P
B 900 1-July-2015 C
B 80  1-Aug-2015 P
C 200 1-Aug-2015 P
D 200 1-Aug-2015 C
;
run;

One of the obnoxious things about including an informat as part of an INPUT statement is the implied length and where the format starts reading from. You may have figured that out when you looked at your data as the STATUS variable had  values of 15 or 5 depending on the line of the data.

 

Trusted Advisor
Posts: 1,204

Re: Date informat

Thanks everyone for your help. I was missing informat statement. 

 

Regards,

Super User
Super User
Posts: 6,502

Re: Date informat


stat_sas wrote:

Thanks Reeza. I tried but its not working. Please see below the data which I am trying to bring in.

 

data have;
input Product $ Qty Date ANYDTDTE. Status $;
datalines;
A 150 1-June-2015 C
A 120 1-July-2015 P
B 900 1-July-2015 C
B 80   1-Aug-2015 P
C 200 1-Aug-2015 P
D 200 1-Aug-2015 C
;


You need to use the colon modifier so that SAS will continue to read the data lines in list mode instead of formatted mode.

data have;
  input Product $ Qty Date :ANYDTDTE. Status $;
  format date yymmdd10. ;
datalines;
A 150 1-June-2015 C
A 120 1-July-2015 P
B 900 1-July-2015 C
B 80   1-Aug-2015 P
C 200 1-Aug-2015 P
D 200 1-Aug-2015 C
;
PROC Star
Posts: 7,363

Re: Date informat

Another thing that happens when you include an informat statement is that you change the order of the variables in your dataset, placing the one with the informat first. Conversely, just adding a colon would have the same effect. e.g.:

 

data have;
  input Product $ Qty Date : ANYDTDTE. Status $;
  format date date9.;
  datalines;
A 150 1-June-2015 C
A 120 1-July-2015 P
B 900 1-July-2015 C
B 80   1-Aug-2015 P
C 200 1-Aug-2015 P
D 200 1-Aug-2015 C
E 300 15-Dec-2015 C
;

Art, CEO, AnalystFinder.com

Super User
Super User
Posts: 6,502

Re: Date informat

@art297 

You can place the INFORMAT statement after the INPUT statement since it is not executable. That way it will not change the order that the variables are defined.

 

The reason your INPUT statement worked and the original one didn't is that you added the colon modifier.  When you did that it changed the INPUT statement from formatted mode to list mode.  When SAS is reading variables in list mode it will ignore the width specified on the applicable informat and instead read as many characters as appear in the next word in the input stream.

 

Personally I like to explicitly define the variables instead of depending on the side effect of how they are first referenced.

 

Plus using INFORMAT or FORMAT statements as if it are the equivalent of defining a variable's type and length via a LENGTH or ATTRIB statement just makes novice users confused about the meaning of formats and informats in the SAS language.  Using FORMAT statements that way makes it look like the format controls how the variable is defined.  A FORMAT is just instructions for how to display a variable's value. And INFORMATs are instructions for how to read text into variable values.

Trusted Advisor
Posts: 1,204

Re: Date informat

Thanks Art & Tom! This is very helpful in understanding the concept in detail.

 

Regards

Ask a Question
Discussion stats
  • 9 replies
  • 194 views
  • 5 likes
  • 5 in conversation