SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 645 views
  • 0 likes
  • 2 in conversation