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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.