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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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