DATA Step, Macro, Functions and more

Converting to SAS Dates

Reply
Occasional Contributor
Posts: 19

Converting to SAS Dates

In serious need of help, struggling with date conversion. I am starting with a list of dates, let's call them start_date and end_date, and they are in the format (e.g. 2016/10/05) YY/MM/DD. I am struggling converting them into a numeric value so that I can then convert them into a SAS date then back into a character value. I have been online for hours looking for some type of explanation and code, but I have had no luck. I have even compressed the variable and taken out the '/', tried renaming it so I could keep original name then used the input statement but still not luck. Even tried using the put statement...please I am so stuck. I have been programming consistently for about a month now and this is my first encounter with dates. 

Occasional Contributor
Posts: 16

Re: Converting to SAS Dates

data test;
format date yymmdd10.;
date = input('2016-01-01',yymmdd10.);run;

Occasional Contributor
Posts: 19

Re: Converting to SAS Dates

This does not work because the "dates" in my orginial data are actually character variables. I need to convert the character "dates" to numeric before I can actually make them into SAS dates. I just can't figure out how...everything I try gives me an error or all missing values 

Super User
Posts: 5,069

Re: Converting to SAS Dates

The suggested solution does work.

 

You may need to plug in the variable names, such as:

 

start_sasdate = input(start_date, yymmdd10.);

 

You can't re-use the same variable name, since a character variable should not be assigned a numeric value.

 

Other possibilities if this is not working for you:  Maybe your existing variables are not actually character.  Maybe that are already SAS dates, with a format being applied for printing purposes.  A PROC CONTENTS will reveal that quickly enough.

Occasional Contributor
Posts: 19

Re: Converting to SAS Dates

I preformed a Proc Contents and this is the result for those variables. 

 

Variable Type Len Informat Label
Start_DateChar50$CHAR50.Start Date
Stop_DateChar50$CHAR50.

Stop Date

Super User
Posts: 5,069

Re: Converting to SAS Dates

Your PROC CONTENTS results show one more possibility to guard against.  With a $CHAR informat, it is possible  your variable contains leading blanks.  So protect against that with:

 

start_sasdate = input(left(start_date), yymmdd10.);

 

Super Contributor
Posts: 394

Re: Converting to SAS Dates

@nash_sas gave you the answer. Here's a somewhat wordier version:

 

data;
date_str = '2016/10/05';
date_num = input(date_str, yymmdd10.);
put 'Numeric date is ' date_num;
put 'Formatted numeric date is ' date_num mmddyy10.;
run;

date_str is your original date as a character string. date_num is its numeric equivalent. Without a format date_num is 20732. If printed using the mmddyy10. format, date_num is 10/05/2016.

Occasional Contributor
Posts: 19

Re: Converting to SAS Dates

this is my code:

 

data test;
     set orig;
     format start_date2 yymmdd10.;
     start_date2 = input(start_date,yymmdd10.);
run;

 

I am still receiving an enormous amount of errors in my log that basically tells me that I have an invalid argument to function input. I may be interrupting the original data wrong, because this is not working. I am still confused as to how you can go directly from a character variable into a SAS date without converting to numeric first. I am sorry, but I am fairly new to SAS.

 

Super User
Posts: 10,458

Re: Converting to SAS Dates

Post the log with the code and the error messages.

 

By any chance do you have dummy values such as 9999/99/99 for missing dates? 99 are not acceptable months or days and will generate errors.

Occasional Contributor
Posts: 19

Re: Converting to SAS Dates

666 data test;
667 set orig;
668 start_date2 = input(start_date, yymmdd10.);
669 format start_date2 yymmdd10.;
670 run;

NOTE: Invalid argument to function INPUT at line 668 column 15.

***this NOTE: Invalid argument is repeated about 10 more times then it goes on to say***

NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to
missing values.
Each place is given by: (Number of times) at (Line)Smiley SadColumn).
34 at 668:15
NOTE: There were 14186 observations read from the data set WORK.orig
NOTE: The data set WORK.TEST has 14186 observations and 36 variables.
NOTE: DATA statement used (Total process time):
real time 0.56 seconds
cpu time 0.10 seconds

Super User
Posts: 5,069

Re: Converting to SAS Dates

This is not necessarily a bad result.  This is saying that out of 14,186 observations, 34 had an incoming date value that wasn't valid.  The log messages will give you more detail about what is actually in those incoming dates.

Super User
Posts: 5,069

Re: Converting to SAS Dates

That's a separate question that could be addressed in due time ... how does the INPUT function convert from character to numeric.  First, though, see if this clears up the problem:

 

data test;
     set orig;
     format start_date2 yymmdd10.;
     start_date2 = input( left(start_date),yymmdd10.);
run;

 

 

Super User
Super User
Posts: 6,495

Re: Converting to SAS Dates

The INPUT() function should let you convert strings to dates.  But you will need to generate a numeric variable to store them as you cannot store them back into the character variable.  So if you have the variable DATEVAR you can make a new variable DATE_NUM that has it as a real date value.

 

date_num = input(datevar,yymmdd10.);
format date_num yymmdd10. ;

If you want the new variable to have the same name again then you will need to rename the variables.

rename datevar=date_char date_num=datevar ;
Occasional Contributor
Posts: 19

Re: Converting to SAS Dates

Okay, so when I look into my contents window it is correctly formatted... Why am I still getting this gigantic amount of errors in my log window? 

Super User
Posts: 5,069

Re: Converting to SAS Dates

[ Edited ]

Well, the log is messy but it doesn't represent a lot of observations.  let's clean it up and see where the problem comes from:

 

data test;
     set orig;
     format start_date2 yymmdd10.;
     start_date2 = input( start_date, ??yymmdd10.);
    if start_date2 = . then put start_date=;

run;

 

It doesn't appear to be the left-hand justification.  So for now I took out the LEFT function.  The ?? will suppress all those messages  you were getting.  And the PUT statement will give you more succinct messages for the problem cases.

 

 

Ask a Question
Discussion stats
  • 16 replies
  • 492 views
  • 0 likes
  • 7 in conversation