Help using Base SAS procedures

Modifying Variables for date values

Reply
Frequent Contributor
Posts: 99

Modifying Variables for date values

Hi all—

I have two questions regarding dates. I know these seem elementary but I’m having trouble understanding the process.

I need to chance date column from to character to numeric and then change the format to date9. I

DATE
3/11/2011
12/7/2011
8/7/2011
12/8/2011
6/7/2012

I am not sure how to import this data correctly so  the variables PRVSVC_CLOSING_DT, M999_ACTUAL_CLOSE_DT,  PROMIS_EVENT_CLOSINGS_ID, CLOSE. Come in as dates with a date9.. 

data test;

input

PROMIS_CASE_EVENTS_ID

PRVSVC_CLOSING_DT

M999_ACTUAL_CLOSE_DT

PROMIS_EVENT_CLOSINGS_ID

CLOSE  ;

cards;

111 .   7-Dec-11    1   7-Dec-11

111 7-Oct-11    .   2   7-Oct-11

111 .   .   3   .

111 .   .   4   .

;run;

Thanks!!!

Super User
Posts: 17,819

Re: Modifying Variables for date values

Try adding an informat statement before the input

informat prvsvc_closing_dt anydtdte9.;

format variable date9.;

input etc... goes after.

Contributor
Posts: 52

Re: Modifying Variables for date values

An informat is needed for reading the dates and the format is needed for displaying the dates. You can associate informats with variables as part of the input statement by using a colon:

data test;

   input

      promis_case_events_id

      prvsvc_closing_dt : anydtdte9.

      m999_actual_close_dt : anydtdte9.

      promis_event_closings_id

      close : anydtdte9.;

   format

      prvsvc_closing_dt

      m999_actual_close_dt

      close date9.;

   cards;

;

run;

Frequent Contributor
Posts: 99

Re: Modifying Variables for date values

Great thanks!

One thing, all the missing values change to 01JAN1960. How can i adjust for this?

Super Contributor
Posts: 1,636

Re: Modifying Variables for date values

data want;

  set test;

  if  prvsvc_closing_dt=. then prvsvc_closing_dt=mdy(01,01,1960);

  if  m999_actual_close_dt=. then m999_actual_close_dt=mdy(01,01,1960);

run;

Contributor
Posts: 66

Re: Modifying Variables for date values

I am confused. Do you mean you don't want missing values to be converted to 01JAN1960 but they are?

Edited to correct: Oops, the below in [...] is incorrect. Thanks, Patrick.

[You must have the system option missing=0 in effect. The beginning of time or 0 in SAS is January 1, 1960.

Change this option by submitting the following statement:

options missing=.;

Hope this helps,]

Dorota

Respected Advisor
Posts: 3,890

Re: Modifying Variables for date values

@Dorota

"Options missing='0';" does not result in missings being printed as 01Jan1960 but as missings being printed as '0'. The option doesn't change the internal values of a variable but only affect how missings are printed. And missings remain missings even when a date format is used to print numeric values.

Contributor
Posts: 66

Re: Modifying Variables for date values

True, indeed. Thanks for your correction. I must have been really tired. Sorry for the confusion.

Super Contributor
Posts: 349

Re: Modifying Variables for date values

Hi,

Try this...

data test;

input PROMIS_CASE_EVENTS_ID PRVSVC_CLOSING_DT M999_ACTUAL_CLOSE_DT :date9.

PROMIS_EVENT_CLOSINGS_ID CLOSE  :date9.;

format M999_ACTUAL_CLOSE_DT CLOSE date9.;

cards;

111 .   7-Dec-11    1   7-Dec-11

111 .   7-Oct-11    2   7-Oct-11

111 .   .           3   .

111 .   .           4   .

;

run;

Thanks,

Shiva

Contributor
Posts: 66

Re: Modifying Variables for date values

Robert,

Dates in SAS are numeric variables indicating number of days since January 1, 1960. Once you enter the date correctly as an integer, you can assign various formats. The values are still the same, but the display changes.

Format date9. is DDMMMYYYY, e.g. the zero day is 01JAN1960. So, if you assign the value of zero, your date will be January 1, 1960 and it is equivalent to assigning the date value equal to '01JAN1960'd.

Your DATE column is displayed with slashes, but the day and month fields do not have leading zeroes, so the slash position is not fixed. This is not date9. format. The length of the string can be between 8 and 10. You can still read it with a date informat though.

data have;

format date_fst date9.;

input date_fst: anydtdte10.;

cards;

3/11/2011

.

12/7/2011

8/7/2011

12/8/2011

.

6/7/2012

;

run;

options nocenter nodate pageno=1;

proc print;run;

  /*  The output will be in date9. format by default,

Obs date_fst
1 11MAR2011
2         .
3 07DEC2011
4 07AUG2011
5 08DEC2011
6         .
7 07JUN2012

  */

but you can change the format in each procedure.

proc print;

format date_fst mmddyy10.;

run;

/*

Obs  date_fst
1 03/11/2011
2          .
3 12/07/2011
4 08/07/2011
5 12/08/2011
6          .
7 06/07/2012

  */

You can change the missing values by assigning zero value to a date variable. This is the same as assigning the '01JAN1960'd value.

data want; set have;

if date_fst=. then date_fst=0;

run;

proc print;run;

/*

Obs date_fst
1 11MAR2011
2 01JAN1960
3 07DEC2011
4 07AUG2011
5 08DEC2011
6 01JAN1960
7 07JUN2012

*/

I hope this helps. Let us know if you have further questions.

Dorota

Ask a Question
Discussion stats
  • 9 replies
  • 255 views
  • 0 likes
  • 7 in conversation