LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (June 2005, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Mon, 6 Jun 2005 07:27:22 -0700
Reply-To:     "Terjeson, Mark (IM&R)" <Mterjeson@RUSSELL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         "Terjeson, Mark (IM&R)" <Mterjeson@RUSSELL.COM>
Subject:      Re: how to convert char date values to date9 format.
Comments: To: m k <odshtml@YAHOO.COM>
Content-Type: text/plain; charset="US-ASCII"

Hi mk,

The PUT() function converts numeric to string. The INPUT() function converts string to numeric.

In both of these a format can be used for the function to parse things out with.

You don't mention the exact string-pattern your string dates are in, but these principles apply regardless.

SAS DATEs are merely a numeric variable that contain the number of days since 1/1/1960, and SAS DATETIMEs are merely a numeric variable that contain the number of seconds since 1/1/1960.

There are a lot of informats and (out)formats and a number of functions for converting these. You can also do the simple arithmetic by hand if you want as well.

The MDY(m,d,y) function is indeed one of these. But as you indicate, the three arguments to the MDY() function want to be numeric arguments.

Date conversion or any other variable, if you get type conversion messages, you can always use the PUT or INPUT functions to convert the type from numeric to string and vice versa.

e.g.

data _null_; string_date = '06/05/2005'; month_str = substr(string_date,1,2); month_num = input(month_str,2.); day_str = substr(string_date,4,2); day_num = input(day_str,2.); year_str = substr(string_date,7,4); year_num = input(year_str,4.); sas_date = mdy(month_num,day_num,year_num); sas_date_wfmt = sas_date; format sas_date_wfmt mmddyy10.; put _all_; run;

of course, functions can be nested:

data _null_; string_date = '06/05/2005'; month_num = input(substr(string_date,1,2),2.); day_num = input(substr(string_date,4,2),2.); year_num = input(substr(string_date,7,4),4.); sas_date = mdy(month_num,day_num,year_num); sas_date_wfmt = sas_date; format sas_date_wfmt mmddyy10.; put _all_; run;

or,

data _null_; string_date = '06/05/2005'; sas_date = mdy(input(substr(string_date,1,2),2.), input(substr(string_date,4,2),2.), input(substr(string_date,7,4),4.)); format sas_date mmddyy10.; put _all_; run;

Even though there are a number of functions to do this many ways, SAS has many, many built-in formats and informats that are very handy. You should check of the documentation of these to see all the different variations.

These are much more handy: (only two of many formats)

data _null_; string_date = '06/05/2005'; sas_date = input(string_date,mmddyy10.); format sas_date mmddyy10.; put _all_; run;

data _null_; string_date = '01JAN2004'; sas_date = input(string_date,date9.); format sas_date date9.; put _all_; run;

Hope this is helpful.

Mark Terjeson Senior Programmer Analyst, IM&R Russell Investment Group

Russell Global Leaders in Multi-Manager Investing

-----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of m k Sent: Monday, June 06, 2005 7:04 AM To: SAS-L@LISTSERV.UGA.EDU Subject: how to convert char date values to date9 format.

Hi

I have a dataset with three char variables deathd, deathm, deathy. I now want to convert them to date values.. One way to do them would be to concatenate them which will work if i just needed to print them, but here I need to compare them with other date variables, and print the greater value for that customer id and print them.

how may i proceed. I even used the mdy function, but then it tells me that the month var which has char values like jan,feb cannot be converted to numeric. and for some reason I cant think straight now, so i need your help..

thanks very much mk

--------------------------------- Do you Yahoo!? Yahoo! Mail - Find what you need with new enhanced search. Learn more.


Back to: Top of message | Previous page | Main SAS-L page