data dates;
format year ddmmyy10. month ddmmyy10.;
do i ='01JAN2022'd to '31DEC2022'd ;
year=year(i) ;
month=month(i);
output;
end; proc print;
run;
Why it show 1960 year instead given range
The YEAR of 01JAN2022 is 2022, and the month is 1. Since you used a DATE format (which expects a SAS date value) the variables will show the 2022nd day after 1960-01-01 and the 1st day after 1960-01-01, respectively.
Remove the FORMAT, and you'll get what you expect:
data dates;
do i = '01JAN2022'd to '31DEC2022'd;
year = year(i);
month = month(i);
output;
end;
run;
proc print;
run;
ABOVE CODE OUTPUT HERE
@Suryak wrote:
ABOVE CODE OUTPUT HERE
Working as expected / coded. If you don't tell us what you want, it is hardly possible to suggest code that fulfils your expectations.
Normally, creating a column containing YEAR value and another column containing a MONTH value is not a good practice. Better you should simply apply the format of MONYY7. (or any similar format) to your variable named I and then your 22646 appears as JAN2022 (if that's what you want).
Yep, works as expected and as it should. If you want your loop variable to be human-readable, apply a format.
If you expected something else, show us what you expected. We can't read your thoughts, especially not over the internet.
data dates;
do _date = '01JAN2022'd to '31DEC2022'd;
year = year(_date);
month = month(_date);
year2=_date;
month2=_date;
output;
end;
format _date date9. year2 year4. month2 monname3.;
run;
proc print data=dates;
run;
Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...
I think you misunderstand the purpose of formats. The purpose of formats is to make the values of variables more readable. In the case of calendar dates, SAS uses the number of days since 01JAN1960. So 01JAN2020 is actually 21915, but no one understands what 21915 means (except SAS, and that's not a person) and so you apply the DATE9. format to this variable and now instead of 21915, you see 01JAN2020, and people understand that, the information is now much more readable. There are other formats available as well, so if (for example) you want 01/2020 or JAN2020, you can make the variable appear the way you want, but still readable.
Do you need to apply formats to a year? If the value of a year variable is 2020, everyone understands what that means, formats won't help (and as you have seen, applying a date format to a value of 2020 gives results that are just plain wrong).
Do you need to apply formats to a month? If the value of a month variable is 9, everyone understands what that means, but you could still apply the MONNAME. format and then 9 appears as SEPTEMBER (but as you have seen, applying the DDMMYY. format to a month value of 9 gives results that are just plain wrong).
So formats do useful things for DATES because it makes them readable. You should not apply formats to values that are already a year value, these are already readable. You can apply month formats to month numbers to make them more readable, but not date formats.
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!
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.