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

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Astounding
PROC Star

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.);

Tom
Super User Tom
Super User

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;

 

sasworker16
Calcite | Level 5

Thank you very much for your help.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 4743 views
  • 2 likes
  • 3 in conversation