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. 

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3233 views
  • 0 likes
  • 5 in conversation