BookmarkSubscribeRSS Feed
Nmsh
Calcite | Level 5

Hey guys,

 

Working with longitudinal data with lots of admission/discharge times. The data is entered into the .csv file as date + military time in the same cell, for example: 01/03/2011 0534.

 

Trying ti get SAS to read the whole entry but it only reads the date part, ignores the time. It autimatically assigns the format 'MMDDYY10' to these date-time columns. Tried to use various date-time informats to no avail, keep getting an error. Also tried to use the 'Picture statement' in PROC FORMAT to customize my own date-time variable. SAS wont budge.

 

Here's a copy of my code:

FILENAME REFFILE '/folders/myshortcuts/SAS/PIMs.csv';

 

/*Define your own date-time format using a date-time "Picture" options*/
proc format;
picture Nada other= '%0m/%0d/%Y %0H%0M' (datatype=datetime);
run;

 

data WORK.PIMs ;
%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
infile REFFILE delimiter = ',' MISSOVER DSD firstobs=2 ;

 

informat Arrv_Date_Time datetime_20. ;
informat Disch_Date_Time MDYAMPM25. ;

 

format Arrv_Date_Time Nada. ;
format Disch_Date_Time Nada. ;

 

input
Arrv_Date_Time
Disch_Date_Time
;
if _ERROR_ then call symputx('_EFIERR_',1); /* set ERROR detection macro variable */
run;

___________________________________________________________________________________________________________

Any input is much appreciated, thanks!

 

Nada

1 REPLY 1
ChrisNZ
Tourmaline | Level 20

SAS easily reads 24h-clock times, but has no provisions for reading times with no separator, so you need to transform the value.

 

Here are two examples of options you have, one uses a custom informat to do the transformation.

 

proc format;  
  invalue val2dtm 's/(.{10}) (\d\d)(\d\d)/\1:\2:\3/' (regexpe)=[anydtdtm.];
run;

data HAVE;
  A='01/03/2011 0534';  
  B=input(A,val2dtm.);    
  putlog B= datetime20.;
run;

data HAVE;
  A='01/03/2011 0534'; 
  B=prxchange('s/(\d\d).(\d\d).(\d{4}).(\d\d)(\d\d)/\3-\1-\2T\4:\5/',1,A);
  C=input(B,e8601dt.);;
  putlog C= datetime20.;
run;

B=01MAR2011:05:03:00
C=03JAN2011:05:34:00

(My locale uses a default data format of dd/mm/yyyy, but the code above translates your date as mm/dd/yyyy since that's what you use)

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 408 views
  • 0 likes
  • 2 in conversation