Good day,
I am trying to extract just the day and Month dd-mmm from a datetime field.
The data is being pulled via Proc SQL; as the data resides on the SQL server.
e.DateOfBirth give .17MAR1957:00:00:00.000 --> need --> 17-Mar
Its formatting in SQL is datetime (length 8 )
Will appreciate any assistance.
Thank you
Hi ColleenCB,
Here is how you can get various flavors of day&month:
data _null_;
length y $6;
x = '17MAR1957:00:00:00.000'dt;
y = put(datepart(x),date5.); /* 17MAR */
put y=;
y = catx('-',substr(y,1,2),substr(y,3)); /* 17-MAR */
put y=;
y = propcase(y); /* 17-Mar */
put y=;
run;
Hope this helps.
Please tell us how you intend to use the value like 17-Mar?
Typically I would use the DATEPART function to create a date value from the datetime and assign an appropriate format. But someone ignoring year may have other thoughts in mind.
You might also need to show how you expect single digit days of the month to appear.
proc format; picture ddmon low-high = '%0d-%3b' (datatype=date) ; run; data example; x="17MAR1957:00:00:00.000"dt; y=datepart(x); format y ddmon.; run;
By creating a date value, as in Y, you preserve the option of changing the display appearance by changing the display format as needed.
I like the formatting & it also works for my problem.
But not required in this exercise.
Thank you so much for answering me.
Hi ColleenCB,
Here is how you can get various flavors of day&month:
data _null_;
length y $6;
x = '17MAR1957:00:00:00.000'dt;
y = put(datepart(x),date5.); /* 17MAR */
put y=;
y = catx('-',substr(y,1,2),substr(y,3)); /* 17-MAR */
put y=;
y = propcase(y); /* 17-Mar */
put y=;
run;
Hope this helps.
Thank you so much for this post.
I will need to keep it save and use it more often - practice makes perfect.
This works perfectly for my requirement.
Have a wonderful day
Do you really need the hyphen? If not just use DTDATE5. format.
426 data test; 427 dob = '17MAR1957:00:00:00.000'dt ; 428 put dob dtdate5. ; 429 run; 17MAR
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.