Hi,
I have a dataset in which the birth date variable is in character format and some of the values do not have the birth month and birth date. Hence, I need to create a new variable in my dataset and just extract the birth year alone. Here is the snippet of the dataset.
Could anyone please help me on how to create a new variable for the birth month alone.
Thanks.
Hi,
You could use the function SUBSTR if all vaues have the same format / layout.
Hi,
data have;
length DOB $32.;
input DOB $;
informat DOB $32.;
datalines;
01/01/1980
01/12/1960
02/01/2019
31/12/2018
;
run;
proc contents data=work.have;
run;
data want;
set have;
new_year=year(input(DOB,ddmmyy10.));
run;
@Riteshdell wrote:
Hi,
data have; length DOB $32.; input DOB $; informat DOB $32.; datalines; 01/01/1980 01/12/1960 02/01/2019 31/12/2018 ; run; proc contents data=work.have; run; data want; set have; new_year=year(input(DOB,ddmmyy10.)); run;
Does not work with the data provided by @sheren_deep1 - if month and day are zero (see first line of the data posted) the format ddmmyy is unabble to create a date, so that year() returns missing.
Using scan+input (as suggested by @Kurt_Bremser), seems be the easiest way to solve the problem:
data want;
set have;
new_year = input(scan(DOB, 3, '/'), 4.);
run;
Hi,
Thanks all for the help. I manage to do it with the substr function.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.