楼主: 3qsir
1676 9

Use month as the counter in macro do looping statement [推广有奖]

  • 2关注
  • 1粉丝

副教授

52%

还不是VIP/贵宾

-

威望
0
论坛币
13606 个
通用积分
27.0809
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
71836 点
帖子
367
精华
0
在线时间
931 小时
注册时间
2011-5-18
最后登录
2021-3-6

楼主
3qsir 发表于 2014-3-16 22:31:23 |AI写论文
20论坛币
Sir : How to apply data merge when we use month as the counter
      in macro do looping statement ,I have two directory in slot c :
      the one c:\RANKING\R192701.xls...R201306.xls,
      the oter c:\HOLDING\H192706.xls...H201312.xls,
      I want merge H192706.xls into R192701.xls(base) id IND every month under lag=6
      from star=192701(starm) to end=201306(endm) , ie R192701+6=H192706
Sincerly !     
R192701
IND    RANKS
B            5
C            4
D            3
E            2
F            1
H192706
IND    SALES
A         99
B         88
C         77
D         66
E         55
F         44
G         33
I          22
I hope result:
C192706
IND     RANKS        SALES
B           5                88
C           4                77
D           3                66
E           2                55
F           1                44
----------------------------------
%MACRO COMBINE(startm, lag, endm);
    %DO i=&startm. %TO &endm.;
              PROC IMPORT DATAFILE="c:\RANKING\R.&i.xls"
              OUT=RR.&i
              DBMS=xls;
              REPLANCE;
              GETNAME=no;
              RUN;
           
             PROC IMPORT DATAFILE="c:\HOLDING\H.&i+6.xls"
             OUT=HH.&i+6
             DBMS=xls;
             REPLANCE;
             GETNAME=no;
             RUN;
            
           DATA CC&.&i+6;
           MERGE RR.&i H.&i+6;
           RUN;
           PROC EXPORT DATA=CC.&i+6
           PUTFILE"C:\COMBINE\C.&i+6.xls"
           DBMS=xls
           REPLACE;
           RUN;
     %END;
%MEND COMBINE(startm, lag, endm);
%COMBINE(192701,6,201306);     
        

最佳答案

yongyitian 查看完整内容

The following macro works for your problem by looping through all the months from startm (starting month) to the endm (end month), and another month of lag 6. It does not work for the lag of other numbers. I am sure you can make it works when puting in the data step and proc steps.This is the log; 583 %macro loopmonth(startm, lag, endm); 584 585 %do m1 = &startm %to &endm; 586 ...
关键词:statement Statemen counter looping Statem counter months star

沙发
yongyitian 发表于 2014-3-16 22:31:24
The following macro works for your problem by looping through all the months
from startm (starting month) to the endm (end month), and another month of lag 6.
It does not work for the lag of other numbers.
I am sure you can make it works when puting in the data step and proc steps.
  1. %macro loopmonth(startm,lag,  endm);
  2.      %do m1 = &startm %to &endm;
  3.         %if %substr(&m1, 5,2) = 13 %then
  4.            %let m1=%eval(%substr(&m1, 1,4)+1)01;

  5.         %if %substr(&m1,5,2) > &lag %then          %do;
  6.            %let tmp=%eval(%substr(&m1,5, 2) + &lag -12);
  7.            %let tmp = %sysfunc(putn(&tmp,z2.));
  8.            %let m2=%eval(%substr(&m1,1,4)+1)&tmp;  %end;
  9.         %else %if %substr(&m1, 5, 2) le &lag %then %let m2=%eval(&m1+&lag);

  10.         %*   put your data-step and procs here;
  11.   %put RFile=R&m1..xls      RRData=RR&m1      HFile=H&m2..xls      HHData=HH&m2 ;

  12.      %end;
  13. %mend;
  14. %loopmonth(192701, 6, 192912);
复制代码
This is the log;
583  %macro loopmonth(startm, lag,  endm);
584
585        %do m1 = &startm %to &endm;
586             %if %substr(&m1, 5,2) = 13 %then
587                %let m1=%eval(%substr(&m1, 1,4)+1)01;
588
589                 %if %substr(&m1,5,2) > &lag %then %do;
590                     %let tmp=%eval(%substr(&m1,5, 2) + &lag -12);
591                     %let tmp = %sysfunc(putn(&tmp,z2.));
592                     %let m2=%eval(%substr(&m1,1,4)+1)&tmp;  %end;
593                  %else %if %substr(&m1, 5, 2) le &lag %then %let m2=%eval(&m1+&lag);
594
595                  %*   put your data-step and procs here;
596   %put RFile=R&m1..xls      RRData=RR&m1      HFile=H&m2..xls      HHData=HH&m2 ;
597
598          %end;
599  %mend;

600  %loopmonth(192701, 6, 192912);
RFile=R192701.xls      RRData=RR192701      HFile=H192707.xls      HHData=HH192707
RFile=R192702.xls      RRData=RR192702      HFile=H192708.xls      HHData=HH192708
RFile=R192703.xls      RRData=RR192703      HFile=H192709.xls      HHData=HH192709
RFile=R192704.xls      RRData=RR192704      HFile=H192710.xls      HHData=HH192710
RFile=R192705.xls      RRData=RR192705      HFile=H192711.xls      HHData=HH192711
RFile=R192706.xls      RRData=RR192706      HFile=H192712.xls      HHData=HH192712
RFile=R192707.xls      RRData=RR192707      HFile=H192801.xls      HHData=HH192801
RFile=R192708.xls      RRData=RR192708      HFile=H192802.xls      HHData=HH192802
RFile=R192709.xls      RRData=RR192709      HFile=H192803.xls      HHData=HH192803
RFile=R192710.xls      RRData=RR192710      HFile=H192804.xls      HHData=HH192804
RFile=R192711.xls      RRData=RR192711      HFile=H192805.xls      HHData=HH192805
RFile=R192712.xls      RRData=RR192712      HFile=H192806.xls      HHData=HH192806
RFile=R192801.xls      RRData=RR192801      HFile=H192807.xls      HHData=HH192807
RFile=R192802.xls      RRData=RR192802      HFile=H192808.xls      HHData=HH192808
RFile=R192803.xls      RRData=RR192803      HFile=H192809.xls      HHData=HH192809
RFile=R192804.xls      RRData=RR192804      HFile=H192810.xls      HHData=HH192810
RFile=R192805.xls      RRData=RR192805      HFile=H192811.xls      HHData=HH192811
RFile=R192806.xls      RRData=RR192806      HFile=H192812.xls      HHData=HH192812
RFile=R192807.xls      RRData=RR192807      HFile=H192901.xls      HHData=HH192901
RFile=R192808.xls      RRData=RR192808      HFile=H192902.xls      HHData=HH192902
RFile=R192809.xls      RRData=RR192809      HFile=H192903.xls      HHData=HH192903
RFile=R192810.xls      RRData=RR192810      HFile=H192904.xls      HHData=HH192904
RFile=R192811.xls      RRData=RR192811      HFile=H192905.xls      HHData=HH192905
RFile=R192812.xls      RRData=RR192812      HFile=H192906.xls      HHData=HH192906
RFile=R192901.xls      RRData=RR192901      HFile=H192907.xls      HHData=HH192907
RFile=R192902.xls      RRData=RR192902      HFile=H192908.xls      HHData=HH192908
RFile=R192903.xls      RRData=RR192903      HFile=H192909.xls      HHData=HH192909
RFile=R192904.xls      RRData=RR192904      HFile=H192910.xls      HHData=HH192910
RFile=R192905.xls      RRData=RR192905      HFile=H192911.xls      HHData=HH192911
RFile=R192906.xls      RRData=RR192906      HFile=H192912.xls      HHData=HH192912
RFile=R192907.xls      RRData=RR192907      HFile=H193001.xls      HHData=HH193001
RFile=R192908.xls      RRData=RR192908      HFile=H193002.xls      HHData=HH193002
RFile=R192909.xls      RRData=RR192909      HFile=H193003.xls      HHData=HH193003
RFile=R192910.xls      RRData=RR192910      HFile=H193004.xls      HHData=HH193004
RFile=R192911.xls      RRData=RR192911      HFile=H193005.xls      HHData=HH193005
RFile=R192912.xls      RRData=RR192912      HFile=H193006.xls      HHData=HH193006
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
3qsir + 5 + 5 + 5 热心帮助其他会员

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

藤椅
farmman60 发表于 2014-3-17 06:20:21

%MACRO COMBINE(startm, lag, endm);
    %DO i=&startm. %TO &endm.;
              PROC IMPORT DATAFILE="c:\RANKING\R&i..xls"
              OUT=RR&i
              DBMS=EXCEL
              REPLANCE;
              RUN;
       %let j=%eval(&i+&lag);   
             PROC IMPORT DATAFILE="c:\HOLDING\H&j..xls"
             OUT=HH&j
             DBMS=EXCEL;
             REPLANCE;
             RUN;
            
           DATA CC&j;
           MERGE RR&i HH&j;
                   BY IND;
           RUN;
           PROC EXPORT DATA=CC&j
           OUTFILE="C:\COMBINE\C&j..xls"
           DBMS=EXCEL
           REPLACE;
           RUN;
     %END;
%MEND COMBINE;


%COMBINE(192701,6,201306);     

板凳
farmman60 发表于 2014-3-17 06:27:22
Sorry to modify codes as follows:

%MACRO COMBINE(startm, lag, endm);
    %DO i=&startm. %TO &endm.;
              PROC IMPORT DATAFILE="c:\RANKING\R&i..xls"
              OUT=RR&i
              DBMS=EXCEL
              REPLANCE;
              RUN;
       %let j=%eval(&i+&lag);   
             PROC IMPORT DATAFILE="c:\HOLDING\H&j..xls"
             OUT=HH&j
             DBMS=EXCEL;
             REPLANCE;
             RUN;
            
           DATA CC&j;
           MERGE RR&i (IN=A) HH&j (IN=B);
            BY IND;
            IF A;
           RUN;
           PROC EXPORT DATA=CC&j
           OUTFILE="C:\COMBINE\C&j..xls"
           DBMS=EXCEL
           REPLACE;
           RUN;
     %END;
%MEND COMBINE;


%COMBINE(192701,6,201306);     

报纸
3qsir 发表于 2014-3-17 09:49:37
WRONG !!! (YOU FOGOT THAT  MONTH IS COUNTER EVERY ROUND 12)
Sir you did not consider month LE 12(Less Then)
so not exist on my excel.file, for example H192713, H192714. H192715...
wehen %j=13>12, H192713--->H192801

地板
dogmamongo 发表于 2014-3-17 10:20:03
sorry, i misunderstand the questionso i delete my code

7
3qsir 发表于 2014-3-17 10:45:02
You are confuse by this question
the month's counter only 1 until 12  

8
farmman60 发表于 2014-3-18 01:37:06
3qsir 发表于 2014-3-17 09:49
WRONG !!! (YOU FOGOT THAT  MONTH IS COUNTER EVERY ROUND 12)
Sir you did not consider month LE 12(Le ...
In your first post, it seemed that your files are continuous, such as R192701.xls to R192712.xls then R192801.xls, R192802..., if so, the code should work.

9
3qsir 发表于 2014-3-18 09:59:04
Hi: you maybe separte with &i into year and conti ,when mod(conti/12)...then I really don't do after this step
when i=192707, lag=6
I hope  R192707 merge  into H192801,
but your %let j=%eval(&i+&lag)----> j=1927707+6=192713
according to your code  R192707 will merge  into H192713
The H192713 is not exist of my H.xls files

10
3qsir 发表于 2014-3-18 10:35:57
I will put in the data step and proc steps
after the coditional statement,
Give me a chance to try it.
Thank for you help .

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-31 17:31