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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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