How to Use the YYYYMM Format in SAS
Display, create, read and group year-month values in SAS with the YYMMN6. format and informat, with practical code and checks to verify the result.
What the YYYYMM format does in SAS
SAS does not have a format literally named YYYYMM. Year-month notation comes from the YYMMxw. family of date formats. For a compact six-digit value with no separator, such as 202403, the format is YYMMN6. In that name, N means "no separator" and 6 is the output width. A matching YYMMN6. informat reads text like 202403 back into a SAS date.
The input to the format is a SAS date value. A SAS date is a number that counts days since 1 January 1960. The format produces only a text representation of that number and leaves the stored value unchanged. You can use it in four ways: to display a date, to convert a date into a YYYYMM string with PUT, to group records by month in procedures, and, through the informat, to turn YYYYMM text back into a real date.
Choosing the separator and width
The letter after YYMM sets the separator. The width sets whether the year is printed with two or four digits. The outputs below are illustrations for the date 15 March 2024.
| Format | Illustrative output | Separator |
| YYMMN6. | 202403 | none |
| YYMMD7. | 2024-03 | dash |
| YYMMS7. | 2024/03 | slash |
| YYMMP7. | 2024.03 | period |
| YYMM7. | 2024M03 | letter M |
| YYMMN4. | 2403 | none, two-digit year |
Specify the width explicitly. A six-digit YYYYMM value needs width 6 when there is no separator and width 7 when there is one. A smaller width shortens the year to two digits, which can make dates from different centuries ambiguous.
Step 1: Display a date as YYYYMM
data work.example;
sale_date = '15MAR2024'd;
format sale_date yymmn6.;
run;
In this example, the FORMAT statement should make SALE_DATE appear as 202403 in reports and table views. The variable remains numeric and still holds the day count for 15 March 2024. Date arithmetic and functions such as INTNX therefore keep working on it.
Step 2: Create a separate YYYYMM variable
data work.example2;
set work.example;
ym_char = put(sale_date, yymmn6.); /* character, e.g. '202403' */
ym_num = year(sale_date)*100 + month(sale_date); /* numeric, e.g. 202403 */
run;
Use the character version for joins with text keys, file names or exports. Use the numeric version when another system expects a number. Neither result is a SAS date. Do not apply date formats to YM_NUM. SAS would read 202403 as a count of days and display an unrelated date.
Step 3: Read YYYYMM text back into a SAS date
data work.parsed;
ym_text = '202403';
month_start = input(ym_text, yymmn6.);
format month_start date9.;
run;
The YYMMN6. informat has no day to read, so the result is the first day of the month. Here MONTH_START should display as 01MAR2024. If the year-month is stored as a number, convert it to text first with input(put(ym_num, 6.), yymmn6.). As an alternative, build the date directly with mdy(mod(ym_num, 100), 1, int(ym_num / 100)).
Step 4: Group or summarize by month
Procedures such as PROC FREQ, and PROC MEANS with a CLASS statement, group by formatted values. Applying YYMMN6. to a daily date therefore combines all days of a month into one group. PROC SQL works differently: its GROUP BY uses stored values, so convert the date with PUT inside the query.
proc sql;
select put(sale_date, yymmn6.) as ym,
count(*) as n
from work.sales
group by calculated ym;
quit;
Using YYYYMM in macro code and file names
%let ym = %sysfunc(today(), yymmn6.);
%let prev_ym = %sysfunc(intnx(month, %sysfunc(today()), -1), yymmn6.);
%put &=ym &=prev_ym;
This puts the current and previous year-month into macro variables. You can use them in names such as sales_&ym..csv. The values depend on the date the code runs. The double period is intentional: the first period ends the macro variable reference, and the second is part of the file name.
How to verify the result
- Run PROC CONTENTS to confirm whether the variable is numeric (a date or plain number) or character.
- Print a few rows with the same date shown in both DATE9. and YYMMN6. to confirm the year and month match.
- Check a January or other single-digit month to confirm the month keeps its leading zero, for example 202401.
- Check the log for invalid-data notes after INPUT, which signal text that is not in YYYYMM form.
- Confirm the format is applied to a real SAS date, not to a number like 202403 that only looks like a year-month.
Limits of this guide
No source documentation or test results were supplied for this article. The behavior described here reflects generally documented SAS date handling, and the code samples are illustrations that were not run for this guide. Width ranges, default widths and any differences between SAS releases or environments are not confirmed here. Check them against the documentation for your SAS version.