DATA Step, Macro, Functions and more

URGENT: extract Year and Month from a date variable

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

URGENT: extract Year and Month from a date variable

Hello,

 

I need a very urgent help:

 

how can I extract Year and Month from a date variable that is formatted as below:

 

dat w.cancer; set c.cancer;

first_chemo_date=input(put(first_chemo_date,8.0,yymmdd8.); format first_chemo_date yymmddn8.;

run;

 

so the date variable looks like this: 

 

first_chemo_date

20150208 

20140722

20110618

.

.

.

 

I tried 

 

year(first_chemo_date) & month(first_chemo_date)

 

and substr command, but they do not work.

 

any help please? this is a very urgent matter.

 

thank you.

 

 


Accepted Solutions
Solution
‎08-02-2016 01:35 AM
Super User
Super User
Posts: 7,076

Re: URGENT: extract Year and Month from a date variable

[ Edited ]
Posted in reply to sasworker16

What error messages and notes did you get when you ran that code?

You are missing a closing ).

If your source variable is NUMERIC then you could do:

data  want ;
  set have ;
  first_chemo_date=input(put(first_chemo_date,Z8.),yymmdd8.);
  format first_chemo_date yymmddn8.;
  yr=year(first_chemo_date);
  mth=month(first_chemo_date);
run;

If your source variable is CHARACTER then you need to make a NEW variable if want to store it as an actual date value.

data want ;
  set have ;
  first_chemo_date_num=input(first_chemo_date,yymmdd8.);
  format first_chemo_date_num yymmddn8.;
  yr=year(first_chemo_date_num);
  mth=month(first_chemo_date_num);
run;

 

View solution in original post


All Replies
Super User
Posts: 5,516

Re: URGENT: extract Year and Month from a date variable

Posted in reply to sasworker16

The issue is that FIRST_CHEMO_DATE is already defined as a character variable.  You can't change it to numeric.  One approach to solving this would be to create a new variable:

 

first_chemo_sasdate=input(put(first_chemo_date,8.0,yymmdd8.); format first_chemo_sasdate yymmddn8.;

 

Then apply the YEAR and MONTH functions to the new variable.  Another alternative would be to read the existing character date.  For example:

 

yearvar = input(first_chemo_date, 4.);

monthvar = input(substr(first_chemo_date, 5), 2.);

Solution
‎08-02-2016 01:35 AM
Super User
Super User
Posts: 7,076

Re: URGENT: extract Year and Month from a date variable

[ Edited ]
Posted in reply to sasworker16

What error messages and notes did you get when you ran that code?

You are missing a closing ).

If your source variable is NUMERIC then you could do:

data  want ;
  set have ;
  first_chemo_date=input(put(first_chemo_date,Z8.),yymmdd8.);
  format first_chemo_date yymmddn8.;
  yr=year(first_chemo_date);
  mth=month(first_chemo_date);
run;

If your source variable is CHARACTER then you need to make a NEW variable if want to store it as an actual date value.

data want ;
  set have ;
  first_chemo_date_num=input(first_chemo_date,yymmdd8.);
  format first_chemo_date_num yymmddn8.;
  yr=year(first_chemo_date_num);
  mth=month(first_chemo_date_num);
run;

 

Contributor
Posts: 24

Re: URGENT: extract Year and Month from a date variable

Thank you very much for your help.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 351 views
  • 0 likes
  • 3 in conversation