☑ This topic is solved.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 08-31-2022 02:25 AM
(1714 views)
Hi.. I'm relatively new and I'm Trying to extract 60 months data from source tables which names as DATA_YYYYMM and created separate data tables.
I wrote the below macro for this.
%MACRO DO_YRMN;
%do YY = 2015 %to 2020;
%do MM = 01 %to 12;
DATA My_DATA_TABLE&YY.&MM
/**** my VAR*****/
SET SOURCE_TABLE&YY.&MM;
RUN;
%END;
%END;
%MEND DO_YRMN;
%DO_YRMN;
Macro runs fine. But the issue is zero is not used. So SAS created code as YYYYM only (20191) instead of YYYYMM(201901)
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
how about this code.
%do MM = 01 %to 12;
%if &MM<10 %then %let MM=0&MM.; /* Add this line */
6 REPLIES 6
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
how about this code.
%do MM = 01 %to 12;
%if &MM<10 %then %let MM=0&MM.; /* Add this line */
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks.. it worked..
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Or do this:
%do mm = 1 %to 12;
%let mm = %sysfunc(putn(&mm.,z2.));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much.. this works tooo.. both the solutions are working.. 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Since you appear to be looping over months it is probably better to use actual dates instead.
%MACRO DO_YRMN;
%local start end offset yymm ;
%let start='01JAN2015'd ;
%let end='01DEC2020'd;
%do offset=0 %to %sysfunc(intck(month,&start,&end));
%let yymm = %sysfunc(intnx(month,&start,&offset),yymmn6.);
DATA My_DATA_TABLE&YYMM
/**** my VAR*****/
SET SOURCE_TABLE&YYMM;
RUN;
%end;
%MEND DO_YRMN;
%DO_YRMN;
Now you can use intervals that are not complete years.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Wow.. that's great.. Thank you so much for the knowledge share.. 🙂