How to read such a date format in SAS?

Accepted Solution Solved
Reply
Regular Contributor
Posts: 190
Accepted Solution

How to read such a date format in SAS?

I have imported an input file, which has date in the form:

20.06.2011

Most of the fields are empty which read like '"-".

how do i get the date out of such format, its currently read in character format


Accepted Solutions
Solution
‎07-08-2014 12:37 PM
SAS Super FREQ
Posts: 8,865

Re: How to read such a date format in SAS?

Posted in reply to munitech4u

Hi:

  You did not explain how you are doing the import (PROC IMPORT, DATA step program) or what your original file is (Excel, text file, CSV, etc).

                

  When you say that "most of the fields are empty" -- usually, SAS would read "empty" fields as missing numeric (a single dot) or missing character (a single space). So a hyphen or dash for missing is odd.

         

  But, if your date is a character string and you want to create a numeric variable with a date value (number of days since Jan 1, 1960), then the INPUT function is what you need to use. For example, let's assume that your character variable was called CHARDATE and that it had a text string of 15.11.1950 (which you wanted to convert to the number -3334, which represents a SAS date value for Nov 15, 1950). Then you would use the INPUT function like this (in a program):

newdatevar= input(chardate,anydtdte10.);

altdatevar = input(chardate,ddmmyy10.);

       
  Where ANYDTDTE or DDMMYY are the "informats" that you need to use to convert your character string INTO a SAS date value with the INPUT function. But if you are using a DATA step program to read your data in the first place, it might be easier to read the data in an INPUT statement like this:

INPUT numvar : anydtdte. ;

  But whichever method you use, you will need an INFORMAT such as ANYDTDTE or DDMMYY in order to read or convert the date value correctly.

     

  Then, the value for the variables would be -3334, internally, and if you wanted to see them as meaningful dates, you would use a SAS format statement like this:

format newdatevar altdatevar mmddyy10.;

           
cynthia

View solution in original post


All Replies
Super User
Posts: 19,791

Re: How to read such a date format in SAS?

Posted in reply to munitech4u

The ddmmyy informat will read this appropriately and specify missing for those that aren't.

How have you specified your informat for this variable?

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

DDMMYYw. Informat

Reads date values in the form ddmmyy<yy> or dd-mm-yy<yy>, where a special character, such as a hyphen (-), period (.), or slash (/), separates the day, month, and year; the year can be either 2 or 4 digits.
Solution
‎07-08-2014 12:37 PM
SAS Super FREQ
Posts: 8,865

Re: How to read such a date format in SAS?

Posted in reply to munitech4u

Hi:

  You did not explain how you are doing the import (PROC IMPORT, DATA step program) or what your original file is (Excel, text file, CSV, etc).

                

  When you say that "most of the fields are empty" -- usually, SAS would read "empty" fields as missing numeric (a single dot) or missing character (a single space). So a hyphen or dash for missing is odd.

         

  But, if your date is a character string and you want to create a numeric variable with a date value (number of days since Jan 1, 1960), then the INPUT function is what you need to use. For example, let's assume that your character variable was called CHARDATE and that it had a text string of 15.11.1950 (which you wanted to convert to the number -3334, which represents a SAS date value for Nov 15, 1950). Then you would use the INPUT function like this (in a program):

newdatevar= input(chardate,anydtdte10.);

altdatevar = input(chardate,ddmmyy10.);

       
  Where ANYDTDTE or DDMMYY are the "informats" that you need to use to convert your character string INTO a SAS date value with the INPUT function. But if you are using a DATA step program to read your data in the first place, it might be easier to read the data in an INPUT statement like this:

INPUT numvar : anydtdte. ;

  But whichever method you use, you will need an INFORMAT such as ANYDTDTE or DDMMYY in order to read or convert the date value correctly.

     

  Then, the value for the variables would be -3334, internally, and if you wanted to see them as meaningful dates, you would use a SAS format statement like this:

format newdatevar altdatevar mmddyy10.;

           
cynthia

🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 1313 views
  • 0 likes
  • 3 in conversation