I'm trying to create a new variable (i.e., a date, numeric) from an existing variable (i.e., a timestamp, character). I can use the SUBSTR function to pull out the date from the timestamp, but I can't figure out the next step.
DATA new; SET old; date=SUBSTR(informed_consent_hip_v_0, 1, 10); RUN;
The timestamp values follow this format: "2021-10-08 12:02:42"
After using the SUBSTR function, the value of "date" is "2021-10-08".
How can I convert "2021-10-08" to a SAS date? Will it automatically convert it to a numeric variable?
Thanks.
Use the Input Function like this
DATA _null_;
dt = "2021-10-08 12:02:42";
date = input(substr(dt, 1, 10), yymmdd10.);
put date = date9.;
RUN;
Use the Input Function like this
DATA _null_;
dt = "2021-10-08 12:02:42";
date = input(substr(dt, 1, 10), yymmdd10.);
put date = date9.;
RUN;
Actually don't even need the substring. The Informat will use the number of characters you specify:
DATA _null_; dt = "2021-10-08 12:02:42"; date = input(dt, yymmdd10.); put date = date9.; RUN;
@ballardw Good catch 🙂
DATA want;
dt = "2021-10-08 12:02:42";
datetime = input(dt, anydtdtm.);
date = datepart(datetime);
put date = date9. datetime = datetime22.;
RUN;
One other option - read in as datetime and parse out the date.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.