- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have a time time variable exported from excel, and saved as character in SAS.
Time_var
12/13/2018 9:59:37.595 AM |
12/13/2018 10:01:17.595 AM |
09/06/2018 9:58:21.513 PM |
..............
I can't use the input function to convert Time_var to a numeric variable since SAS doesn't recognize the format. Any suggestion how to convert this variable into a numeric variable?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data have;
input datetime $30.;
cards;
12/13/2018 9:59:37.595 AM
12/13/2018 10:01:17.595 AM
09/06/2018 9:58:21.513 PM
;
data want;
set have;
numeric_dt=input(datetime,anydtdtm21.);
format numeric_dt datetime20.;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data have;
input datetime $30.;
cards;
12/13/2018 9:59:37.595 AM
12/13/2018 10:01:17.595 AM
09/06/2018 9:58:21.513 PM
;
data want;
set have;
numeric_dt=input(datetime,anydtdtm21.);
format numeric_dt datetime20.;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What number do you want?
You should be able to use ANYDTDTM informat to convert it to a datetime value (number of seconds since 1960). Or the ANYDTDTE informat to convert it to a date value (number of days since 1960). You could even use the ANYDTTME informat to just pull out the time of day value (number of seconds since midnight). Then attach a display format you like.
Note you might have issues with values like the last one where the order of the month and day of month is not clear from the values. The ANYDT.... informats will use the order associated with your current LOCALE settings. If you want to force SAS to use a particular order then you might want to parse the string first. For example to just get the date value you could use:
us_date = input(scan(time_var,1,' '),mmddyy10.);
uk_date = input(scan(time_var,1,' '),ddmmyy10.);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content