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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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;

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

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;
ballardw
Super User

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;

 

Reeza
Super User
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. 

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 1498 views
  • 6 likes
  • 4 in conversation