BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
David_Billa
Rhodochrosite | Level 12

In my program we've date values like 3/25/2022 and 04/24/2082 in two different fields from the excel. After executing the similar step as shown below in my program, it is producing the values like 03/26/2082 and 04/25/2082 in the result. Excepted result is same value as input (03/25/2022) but with SAS date format.

 

Excel which we're using was mess, so we've converted all the fields to character and then reading it from excel. I could not succeed with the below program if I tried to replicate the same step by passing the hardcoded value in the input function. Any help?

 

data have;	
	ORIG_STOP_SHIP = input('3/25/2022',best.);
	EXT_STOP_SHIP = input('4/24/2022',best.);
	format ORIG_STOP_SHIP EXT_STOP_SHIP  mmddyy10.;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

As far as your conversion of character strings to SAS dates, you have to use the proper informat, which in this case is the MMDDYY informat.

 

data have;	
	ORIG_STOP_SHIP = input('3/25/2022',mmddyy10.);
	EXT_STOP_SHIP = input('4/24/2022',mmddyy10.);
	format ORIG_STOP_SHIP EXT_STOP_SHIP  mmddyy10.;
run;

 

In general, converting Excel dates to SAS dates involves subtracting about 60 years. You can use your favorite internet search engine to find the details.

--
Paige Miller

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

To convert a date string to a date, you must use a date informat. BEST. is for "normal" numbers and interprets digits, commas, periods, and scientific notation, but not dates or times.

data have;	
ORIG_STOP_SHIP = input('3/25/2022',mmddyy10.);
EXT_STOP_SHIP = input('4/24/2022',mmddyy10.);
format ORIG_STOP_SHIP EXT_STOP_SHIP mmddyy10.;
run;

Hint: for consistent readability of code, do not use tabs. Only use blanks for whitespace and indentation (except in DATALINES, when you want to use 09x as delimiter).

PaigeMiller
Diamond | Level 26

As far as your conversion of character strings to SAS dates, you have to use the proper informat, which in this case is the MMDDYY informat.

 

data have;	
	ORIG_STOP_SHIP = input('3/25/2022',mmddyy10.);
	EXT_STOP_SHIP = input('4/24/2022',mmddyy10.);
	format ORIG_STOP_SHIP EXT_STOP_SHIP  mmddyy10.;
run;

 

In general, converting Excel dates to SAS dates involves subtracting about 60 years. You can use your favorite internet search engine to find the details.

--
Paige Miller
ballardw
Super User

As an aside: dealing with dates from Excel you may want to consider investigating if the entire column is the same type before any "import" if reading directly from Excel. I have received data in Excel that mixes of Excel date values and strings. Which can behave poorly if "imported".

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
  • 3 replies
  • 510 views
  • 2 likes
  • 4 in conversation