BookmarkSubscribeRSS Feed
trt3s
Calcite | Level 5

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. 

16 REPLIES 16
nash_sas
Fluorite | Level 6

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

trt3s
Calcite | Level 5

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 

Astounding
PROC Star

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.

trt3s
Calcite | Level 5

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

Astounding
PROC Star

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.);

 

Tim_SAS
Barite | Level 11

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

trt3s
Calcite | Level 5

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.

 

ballardw
Super User

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.

trt3s
Calcite | Level 5
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):(Column).
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

Astounding
PROC Star

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.

Astounding
PROC Star

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;

 

 

Tom
Super User Tom
Super User

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 ;
trt3s
Calcite | Level 5

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? 

Astounding
PROC Star

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.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 16 replies
  • 2729 views
  • 0 likes
  • 7 in conversation