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.
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;
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,yy
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.);
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;
Thank you very much for your help.
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.
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.