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

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. 

 

Capture.PNG

 

Could anyone please help me on how to create a new variable for the birth month alone. 

 

Thanks. 

1 ACCEPTED SOLUTION

Accepted Solutions
DJongman
Obsidian | Level 7

Hi,

 

You could use the function SUBSTR if all vaues have the same format / layout.

 

SUBSTR Function

 

 

View solution in original post

5 REPLIES 5
DJongman
Obsidian | Level 7

Hi,

 

You could use the function SUBSTR if all vaues have the same format / layout.

 

SUBSTR Function

 

 

Riteshdell
Quartz | Level 8

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; 
andreas_lds
Jade | Level 19

@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;
sheren_deep1
Calcite | Level 5

Hi, 

 

Thanks all for the help. I manage to do it with the substr function. 

 

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2744 views
  • 0 likes
  • 5 in conversation