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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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');
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

What is ANOVA?

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.

Discussion stats
  • 2 replies
  • 813 views
  • 1 like
  • 3 in conversation