BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sylvia106
Fluorite | Level 6

Hi, 

 

I have character variables like these:
1/25/2017 4:49:01 PM

12/1/2017 12:07:00 PM

 

I want to convert the date part into SAS date. The problem is there are no "0" before months and days (for example they should be like "01/25/2017 4:49:01 PM"). Since MDY are not in certain digits, I can't use "SASDATE = MDY(SUBSTR......" to extract the date properly.

 

I know it sounds simple but I can't find a solution so far. Hope there's a way to solve this!

 

Thanks a lot!

1 ACCEPTED SOLUTION
7 REPLIES 7
georgel
Quartz | Level 8

Hi please try this :I just made some nesting for fiinding the "/" 

data tocorrect;
input date_to_amend $30. ;
datalines;
1/25/2017 4:49:01 PM
12/1/2017 12:07:00 PM

;
run;

data corrected ;
   set  tocorrect;
    format  sasdate DATE9.;
   format month 8.;
   format day 8.;
   format year 8.;
   month=substr(date_to_amend,1, find(date_to_amend,"/")-1);
   day=substr(substr(date_to_amend, find(date_to_amend,"/")+1,length(date_to_amend)),1, find(substr(date_to_amend, find(date_to_amend,"/")+1,length(date_to_amend)),"/")-1);
   year = substr(substr(date_to_amend,1, find(date_to_amend," ")-1),length(substr(date_to_amend,1, find(date_to_amend," ")-1))-3,4);
   sasdate=mdy(month,day,year);
run;
sylvia106
Fluorite | Level 6
Thank you!! This one took me some time to understand but it works! I think this method is useful for dealing with messy data 🙂
sylvia106
Fluorite | Level 6
Thank you so much!! It works well with just some simple lines!
singhsahab
Lapis Lazuli | Level 10
data have;
input date_to_amend $30. ;
datalines;
1/25/2017 4:49:01 PM
12/1/2017 12:07:00 PM
;
run;

data want;
set have;
New_Date=PUT(input(scan(date_to_amend,1,' '),MMDDYY10.),MMDDYY10.)||' '||strip(scan(date_to_amend,2,' ')||' '||scan(date_to_amend,3,' '));
run;
novinosrin
Tourmaline | Level 20

Hi @sylvia106  Sorry late to the party. Your pattern seems pretty straight forward and convenient to just read the datepart  with the appropriate informat. In your sample, the date part of the date-time character value appears to be in the form MMDDYY. 

 

What you could do is, just read upto 10 bytes of character by specifying the informat width as 10 i.e. mmddyy10. . Internally, SAS reads 10 bytes of char which in other words is a non-standard date. The pre-compiled INFORMAT will look up i.e. assign the appropriate integer date value. Then, you can of course assign a format of your choice. 


data tocorrect;
input date_to_amend $30. ;
datalines;
1/25/2017 4:49:01 PM
12/1/2017 12:07:00 PM
;
run;

data want;
 set tocorrect;
 sas_date=input(date_to_amend,mmddyy10.);
 format sas_date date9.;
run  

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 887 views
  • 5 likes
  • 6 in conversation