Hi,
I have the following data-set with first three variables, and I want to create another variable month such that it represents the month and year of the date variable. Any suggestions on how to create it?
winid | permno | date | month |
1 | 1 | 1/1/2000 | Jan-00 |
1 | 1 | 1/2/2000 | Jan-00 |
1 | 1 | 1/3/2000 | Jan-00 |
1 | 1 | ….. | Jan-00 |
1 | 1 | ….. | Jan-00 |
1 | 1 | ….. | Jan-00 |
1 | 1 | 1/31/2000 | Jan-00 |
2 | 1 | 2/1/2000 | Feb-00 |
2 | 1 | 2/2/2000 | Feb-00 |
2 | 1 | …. | Feb-00 |
2 | 1 | ….. | Feb-00 |
2 | 1 | 2/29/2000 | Feb-00 |
Not sure how mabny ways there is to type this: Post test data in the form of a datastep!!!
So now lets start the guessing, is date an actual numeric date value? If so then:
data want; set have; month=put(date,monyy.); run;
Not sure how mabny ways there is to type this: Post test data in the form of a datastep!!!
So now lets start the guessing, is date an actual numeric date value? If so then:
data want; set have; month=put(date,monyy.); run;
For a very large number of analysis or display purposes what you propose doing may not be needed at all. If the "date" is an actual SAS date value and not character then a change of the display format for a procedure may be all that is needed. See this example:
data example; informat date mmddyy10. ; format date mmddyy10.; input date value; datalines; 01/01/2018 123 01/23/2018 24 01/31/2018 456 02/02/2018 333 02/05/2018 6 ; run; proc means data=example; class date; var value; run; proc means data=example; class date; var value; format date monyy5.; run;
No additional variable was needed to get different groups for analysis, just a change of format.
Almost all of the analysis, grahping and reporting procedures like Report and Tabulate will honor the format for creating groups.
Also Proc Format will allow you create specific date, time or datetime appearances if you can't find a SAS supplied format that appears exactly as you want.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.