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

Hello team,

I have the coes as below:

 

LibName MyLibName 'Path';
Infile "Path";
Input Id$ sex$ DOB pdX Dx_3 Dx_4;
Informat DOB Date9.;

 

I know SAS sees the date as number of days away far from January 1st, 1960.

The file from which I have imported data into SAS has this format for DOB: mm-dd-yy.

 

We put informat function in declaration part of the code, which is to say in input statement.  What is the role of it? Does informat change mm-dd-yy to a number so SAS can understand DOB?

 

Please advise me.

Blue Blue Sky

 

 

 

Blue Blue
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@GN0001 

"When SAS reads the data in PDV, what does informat play in this scenario?"

Some docu here - may-be too much for you right now.

 

When SAS reads an external file then:

1. Reads a record into the input buffer

2. Reads the data in the input buffer into the PDV

 

The input buffer just got one record from your external data "as is" loaded into memory. 

The PDV got one row/obs of your data in SAS variables.

Options in the infile statement together with informats provide to SAS the instructions how to map (and convert) the data in the input buffer into SAS variables.

 

Let's say you've got in your external data a string like 05May2021

In the input buffer this is still 05May2021

In your input statement you've got now something like: mydate date9.

The date9. informat provides to SAS the instruction how to read the string 05May2021 into SAS variable mydate. Using this informat SAS will convert the string 05May2021 into a SAS Date value which is the count of days since 1/1/1960 (=22405).

 

A format instructs SAS how to print a value. Taking above example if you just print the values stored in variable mydate without a format then you will get 22405 - which is not human readable. 

A format of date9. (same name like the informat but you could also use some different format like ddmmyy10.) applied to variable mydate will print the same internal value of 22405 as 05May2021.

 

I hope above was helpful and didn't just confuse you.

View solution in original post

9 REPLIES 9
mkeintz
PROC Star

Here is a sample program you can use to answer your own question:

 

data _null_;
  input date ;
  informat date mmddyy10.;
  put 'Using no assigned format: ' @28 date=;
  put 'Using date9 format: '       @28 date=date9.;
  put 'Using mmddyy10. format: '   @28 date=mmddyy10.;
datalines;
04/24/2021
run;

which generates on the log:

 

Using no assigned format:  date=22394
Using date9 format:        date=24APR2021
Using mmddyy10. format:    date=04/24/2021
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

You can see, by using the informat mmddyy10., SAS converted the external string "04/24/2021" to an internal value (22394), which appears on the log according to the assigned format (not informat).used in the PUT statement.  

 

 

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

--------------------------
GN0001
Barite | Level 11
Hello Mkeintz,

What is the role informat here? Does it convert a date such as 04-22-2021 to number so SAS can read the date variable? If we remove informat statement, sas is not able to read the date. The log shows ..
I found this: SAS Informat Statement SAS Informat is an instruction that SAS used to read data values into a variable.
I know format displays date in a way that we can understand.

Regards,
Blue Sky
Blue Blue
Tom
Super User Tom
Super User

@GN0001 wrote:
Hello Mkeintz,

What is the role informat here? Does it convert a date such as 04-22-2021 to number so SAS can read the date variable? If we remove informat statement, sas is not able to read the date. The log shows ..
I found this: SAS Informat Statement SAS Informat is an instruction that SAS used to read data values into a variable.
I know format displays date in a way that we can understand.

Regards,
Blue Sky

Your file does not have a date, it has a string of text characters.   The role of the INFORMAT statement is to attach an informat specification to a variable.  So that when the INPUT statement does not explicitly state what informat to use for that variable the INPUT statement will use the informat that is attached to the variable.

 

Remember:  Informats convert text to values.  Formats convert values to text.

 

Kurt_Bremser
Super User

First of all, you have an INFORMAT statement, not a function (there is no INFORMAT function).

You need to use an informat that matches the format of your date.

DATE9 expects ddmmmyyyy, where the month is written as a 3-character abbreviation. From your description, I take it that you should use the MMDDYY8. informat.

Which idiot sends you dates with 2-digit years? With 4-digit years, use MMDDYY10.

GN0001
Barite | Level 11
Hello Kurtbremser,
I have a course in SAS and the instructor says it is a function as far as I hear it. I agree with you, it is not a function.
Thanks for your response. Your explanation that "the informat that matches the format of my date:" is helpful.
Now, I understood what informat does, I think I need to understand some from the backend of SAS. When SAS reads the data in PDV, what does informat play in this scenario?
Regards,
Blue Blue Sky
Blue Blue
Kurt_Bremser
Super User

@GN0001 wrote:
When SAS reads the data in PDV, what does informat play in this scenario?

The informat is a template that tells SAS how to convert a part of the input stream to a data value for use in the data step. Some are very simple (e.g. $10., which reads the next 10 characters), others quite complex (like the date, time and datetime informats). $10. will read anything, while MMDDYY10. expects a valid date (although it tolerates a wide range of separators and dates shorter than 10 characters).

data check;
input date mmddyy10.;
format date yymmdd10.;
datalines;
02-03-2021
02/03/2021
02 03 2021
02.03.2021
02_03_2021
02:03:2021
2/3/2021
02032021
232021
;

(only the last one will be rejected)

 

If your instructor insists on calling INFORMAT a function, direct her/him here, and ask where the function can be found 😉

 

GN0001
Barite | Level 11
Thanks for your all inputs that got me closer to what I was looking for.
Blue Blue Sky
Blue Blue
Patrick
Opal | Level 21

@GN0001 

"When SAS reads the data in PDV, what does informat play in this scenario?"

Some docu here - may-be too much for you right now.

 

When SAS reads an external file then:

1. Reads a record into the input buffer

2. Reads the data in the input buffer into the PDV

 

The input buffer just got one record from your external data "as is" loaded into memory. 

The PDV got one row/obs of your data in SAS variables.

Options in the infile statement together with informats provide to SAS the instructions how to map (and convert) the data in the input buffer into SAS variables.

 

Let's say you've got in your external data a string like 05May2021

In the input buffer this is still 05May2021

In your input statement you've got now something like: mydate date9.

The date9. informat provides to SAS the instruction how to read the string 05May2021 into SAS variable mydate. Using this informat SAS will convert the string 05May2021 into a SAS Date value which is the count of days since 1/1/1960 (=22405).

 

A format instructs SAS how to print a value. Taking above example if you just print the values stored in variable mydate without a format then you will get 22405 - which is not human readable. 

A format of date9. (same name like the informat but you could also use some different format like ddmmyy10.) applied to variable mydate will print the same internal value of 22405 as 05May2021.

 

I hope above was helpful and didn't just confuse you.

GN0001
Barite | Level 11
Patrick,
This is nicely explained, thanks for it.
Blue Blue Sky
Blue Blue

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 9 replies
  • 826 views
  • 5 likes
  • 5 in conversation