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

HI, All:

I have a SAS data where a date column is a mixture of Date and date/time, and the column is a string. the column looks like below:

1/4/2017

1/31/2017

1/10/20176:18:22AM

1/10/201712:18:28AM

1/3/201711:08:58AM

1/3/201711:9:1AM

1/30/20177:53:21AM

 

I want to seperate only the mm/dd/yy information from the string. All the year are 2017, is that possible to use 2017 as a delimiter to get MM/DD/YYYY from the strings? How to do it?

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

You can do it by:

 

data want;
    infile cards;
    input dtime $;

   date_part = substr(dtime,1,index(dtime,'2017')+3); 
   date_in = input(date_part, ddmmyy10.);

cards;
1/4/2017
1/31/2017
1/10/20176:18:22AM
1/10/201712:18:28AM
1/3/201711:08:58AM
1/3/201711:9:1AM
1/30/20177:53:21AM
; run;

 

 

View solution in original post

6 REPLIES 6
Shmuel
Garnet | Level 18

You can do it by:

 

data want;
    infile cards;
    input dtime $;

   date_part = substr(dtime,1,index(dtime,'2017')+3); 
   date_in = input(date_part, ddmmyy10.);

cards;
1/4/2017
1/31/2017
1/10/20176:18:22AM
1/10/201712:18:28AM
1/3/201711:08:58AM
1/3/201711:9:1AM
1/30/20177:53:21AM
; run;

 

 

lovedieer
Calcite | Level 5

Thank you very much. Finally i can get the date out.

art297
Opal | Level 21

Here is one way:

data want;
  input @;
  sub=index(_infile_,'2017')+3;
  _infile_=substr(_infile_,1,sub);
  input date : mmddyy10.;
  format date date9.;
  cards;
1/4/2017
1/31/2017
1/10/20176:18:22AM
1/10/201712:18:28AM
1/3/201711:08:58AM
1/3/201711:9:1AM
1/30/20177:53:21AM
;

Art, CEO, AnalystFinder.com

lovedieer
Calcite | Level 5

thank you. it help me to pull the date out from the long string.

collinelliot
Barite | Level 11

There's probably many ways to do this, but this is what I'd to to get the date and time extracted. 

 

data dates;
format date mmddyy. time timeampm.;
input @1 dateTxt $32.;
dateTxt = prxchange('s/2017/2017~/', 1, dateTxt);
date = input(scan(dateTxt, 1, '~'), mmddyy10.);
time = input(scan(dateTxt, 2, '~'), time10.);
datalines;
1/4/2017
1/31/2017
1/10/20176:18:22AM
1/10/201712:18:28AM
1/3/201711:08:58AM
1/3/201711:9:1AM
1/30/20177:53:21AM
;
Jagadishkatam
Amethyst | Level 16

alternatively with call prxsubstr

 

data dates;
format date mmddyy. time timeampm.;
input @1 dateTxt $32.;
prxid=prxparse('/\d{1,2}\/\d{1,2}\/\d{2,4}/');
call prxsubstr(prxid,datetxt,start,length);
date=input(substr(dateTxt,start,length),mmddyy10.);
time=input(substr(dateTxt,length+1),time.);
datalines;
1/4/2017
1/31/2017
1/10/20176:18:22AM
1/10/201712:18:28AM
1/3/201711:08:58AM
1/3/201711:9:1AM
1/30/20177:53:21AM
;
Thanks,
Jag

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
  • 6 replies
  • 4687 views
  • 1 like
  • 5 in conversation