SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
stat_sas
Ammonite | Level 13

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,

 

 

9 REPLIES 9
Reeza
Super User

ANYDTDTE.

stat_sas
Ammonite | Level 13

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
;

Reeza
Super User

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
;


 

ballardw
Super User

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.

 

stat_sas
Ammonite | Level 13

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

 

Regards,

Tom
Super User Tom
Super User

@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
;
art297
Opal | Level 21

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

Tom
Super User Tom
Super User

@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.

stat_sas
Ammonite | Level 13

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

 

Regards

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2009 views
  • 5 likes
  • 5 in conversation