BookmarkSubscribeRSS Feed
Santhosh01382
Calcite | Level 5

I have a dataset that has the column:

BRTHDTC - Character

1988-02-23
1973

 

I am attempting to get a numerical BRTHDTC  that takes the column BRTHDT in date9. format.

BRTHDT

23FEB1998

1973

 

Any tips on how to get my desired date field?

 

Thanks,

Kumar

2 REPLIES 2
PaigeMiller
Diamond | Level 26

If the output should be 23FEB1998 in one case and 1973 in another case, I think the best thing you can do is leave the variables as character.

 

If the output should be 23FEB1998 in one case and 01JAN1973 in another case, I think then you could easily read the results into a numeric variable.

 

data have;
    dt='1988-02-23'; output;
    dt='1973'; output;
run;
data want;
    set have;
    num_dt=input(cats(dt,'-01-01'),yymmdd10.);
    format num_dt date9.;
run;
--
Paige Miller
ballardw
Super User

To create a SAS date value where the Date9 or any other date format will apply correctly, you need a day, month and year value. What day and month do you expect to have in the result when only a year , apparently, is supplied? Are the year only values always provided as 4 digits?

Do you have any other layouts for that character date value? We may need to see more to get code that works more completely.

 

This provides an example of how to read those two values into dates. You get to pick the month and day to use as the default for the MDY function that turns numeric month, day and year into a date. I used January 1 for the example.

data example;
   input BRTHDTC :$15.;
   if length(BRTHDTC)=4 then brthdt = mdy(1,1, input(BRTHDTC,4.));
   else brthdt=input(BRTHDTC,yymmdd10.);
   format brthdt date9.;
datalines;
1988-02-23
1973
;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 385 views
  • 0 likes
  • 3 in conversation