I have the variable "date" whose format is in yyyymmdd, but I want to make a new variable "year_month" using only yyyymm. What code can I use?
So you have a variable that contains the number of days since 1960 and is being displayed using the YYMMDDN8. format and you want to make a new variable that is being displayed using the YYMMN6. format?
You could just store the same number and display it differently.
data want;
set have;
year_month=date;
format year_month yymmn8.;
run;
But then some of the values that look (print) the same will actually be stored as different numbers.
Instead you might want to force the value in YEAR_MONTH to always be the first day of the month.
year_month=intnx('month',date,0,'b');
So you have a variable that contains the number of days since 1960 and is being displayed using the YYMMDDN8. format and you want to make a new variable that is being displayed using the YYMMN6. format?
You could just store the same number and display it differently.
data want;
set have;
year_month=date;
format year_month yymmn8.;
run;
But then some of the values that look (print) the same will actually be stored as different numbers.
Instead you might want to force the value in YEAR_MONTH to always be the first day of the month.
year_month=intnx('month',date,0,'b');
If your plan is to perform some statistical analysis of these months, for example in PROC MEANS or PROC SUMMARY or PROC FREQ (or many other PROCs), you don't need to create a new variable, or even change the formatting in the data set. You can change the formatting in the PROC. Example:
proc means data=have;
class date;
format date yymm7.;
var some_variable_name;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.