Hello
I have a data set with wide structure and each customer id has one row with multiple columns of RevRatio (Each column represent another month. Month1 is last month, month 2 is one month before last month and so on)
I want to calculate the change in PCT between each month and next month and also calculate indicators.
IS there another way to calculate it instead of the macro or the macro I wrote is the best way?
Data have;
input CustID RevRatio1-RevRatio12;
cards;
1 0.9 0.8 0.8 0.7 0.9 0.9 0.7 0.6 0.9 0.9 0.7 0.6
2 0.6 0.1 0.2 0.3 0.2 0.4 0.5 1.1 0.9 0.7 0.8 0.9
3 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
4 -9 0.2 0.1 0.4 0.5 -9 -8 0.1 0.2 0.3 0.4 0.1
;
Run;
/*********** Way1-write it without macro****************/
/*********** Way1-write it without macro****************/
/*********** Way1-write it without macro****************/
%let Ratio=RevRatio;
Data want;
set have;
IF &Ratio.2 not in (0,-6,-7,-8,-9,.) and &Ratio.1 not in (-6,-7,-8,-9,.) then PCT_change_2_1=(&Ratio.1-&Ratio.2)/&Ratio.2;
IF &Ratio.3 not in (0,-6,-7,-8,-9,.) and &Ratio.2 not in (-6,-7,-8,-9,.) then PCT_change_3_2=(&Ratio.2-&Ratio.3)/&Ratio.3;
IF &Ratio.4 not in (0,-6,-7,-8,-9,.) and &Ratio.3 not in (-6,-7,-8,-9,.) then PCT_change_4_3=(&Ratio.3-&Ratio.4)/&Ratio.4;
IF &Ratio.5 not in (0,-6,-7,-8,-9,.) and &Ratio.4 not in (-6,-7,-8,-9,.) then PCT_change_5_4=(&Ratio.4-&Ratio.5)/&Ratio.5;
IF &Ratio.6 not in (0,-6,-7,-8,-9,.) and &Ratio.5 not in (-6,-7,-8,-9,.) then PCT_change_6_5=(&Ratio.5-&Ratio.6)/&Ratio.6;
IF &Ratio.7 not in (0,-6,-7,-8,-9,.) and &Ratio.6 not in (-6,-7,-8,-9,.) then PCT_change_7_6=(&Ratio.6-&Ratio.7)/&Ratio.7;
IF &Ratio.8 not in (0,-6,-7,-8,-9,.) and &Ratio.7 not in (-6,-7,-8,-9,.) then PCT_change_8_7=(&Ratio.7-&Ratio.8)/&Ratio.8;
IF &Ratio.9 not in (0,-6,-7,-8,-9,.) and &Ratio.8 not in (-6,-7,-8,-9,.) then PCT_change_9_8=(&Ratio.8-&Ratio.9)/&Ratio.9;
IF &Ratio.10 not in (0,-6,-7,-8,-9,.) and &Ratio.9 not in (-6,-7,-8,-9,.) then PCT_change_10_9=(&Ratio.9-&Ratio.10)/&Ratio.10;
IF &Ratio.11 not in (0,-6,-7,-8,-9,.) and &Ratio.10 not in (-6,-7,-8,-9,.) then PCT_change_11_10=(&Ratio.10-&Ratio.11)/&Ratio.11;
IF &Ratio.12 not in (0,-6,-7,-8,-9,.) and &Ratio.11 not in (-6,-7,-8,-9,.) then PCT_change_12_11=(&Ratio.11-&Ratio.12)/&Ratio.12;
IF PCT_change_2_1>0.05 AND &Ratio.1>0.5 then Ind_2_1=1; /****השינוי מחודש קודם גדול מ5% והיחס בחודש יעד גדול מ50%****/
else IF &Ratio.1>1 and &Ratio.2<1 then Ind_2_1=1;/***מעבר ממצב של לא חורג למצב של חורג***/
else IF &Ratio.2 ne -9 and &Ratio.1 = -9 then Ind_2_1=1; /***בחודש יעד מנצל לא מסגרת ובחודש קודם במצב אחר****/
else IF &Ratio.1>0.5 AND &Ratio.2 in (0,-8,-9) then Ind_2_1=1; /***יחס בחודש יעד גדול מ50% ובחודש קודם יחס היה 0 או מינוס 99999,99998***/
else Ind_2_1=0;
IF PCT_change_3_2>0.05 AND &Ratio.2>0.5 then Ind_3_2=1;
else IF &Ratio.2>1 and &Ratio.3<1 then Ind_3_2=1;
else IF &Ratio.3 ne -9 and &Ratio.2 = -9 then Ind_3_2=1;
else IF &Ratio.2>0.5 AND &Ratio.3 in (0,-8,-9) then Ind_3_2=1;
else Ind_3_2=0;
IF PCT_change_4_3>0.05 AND &Ratio.3>0.5 then Ind_4_3=1;
else IF &Ratio.3>1 and &Ratio.4<1 then Ind_4_3=1;
else IF &Ratio.4 ne -9 and &Ratio.3 = -9 then Ind_4_3=1;
else IF &Ratio.3>0.5 AND &Ratio.4 in (0,-8,-9) then Ind_4_3=1;
else Ind_4_3=0;
IF PCT_change_5_4>0.05 AND &Ratio.4>0.5 then Ind_5_4=1;
else IF &Ratio.4>1 and &Ratio.5<1 then Ind_5_4=1;
else IF &Ratio.5 ne -9 and &Ratio.4 = -9 then Ind_5_4=1;
else IF &Ratio.4>0.5 AND &Ratio.5 in (0,-8,-9) then Ind_5_4=1;
else Ind_5_4=0;
IF PCT_change_6_5>0.05 AND &Ratio.5>0.5 then Ind_6_5=1;
else IF &Ratio.5>1 and &Ratio.6<1 then Ind_6_5=1;
else IF &Ratio.6 ne -9 and &Ratio.5 = -9 then Ind_6_5=1;
else IF &Ratio.5>0.5 AND &Ratio.6 in (0,-8,-9) then Ind_6_5=1;
else Ind_6_5=0;
IF PCT_change_7_6>0.05 AND &Ratio.6>0.5 then Ind_7_6=1;
else IF &Ratio.6>1 and &Ratio.7<1 then Ind_7_6=1;
else IF &Ratio.7 ne -9 and &Ratio.6 = -9 then Ind_7_6=1;
else IF &Ratio.6>0.5 AND &Ratio.7 in (0,-8,-9) then Ind_7_6=1;
else Ind_7_6=0;
IF PCT_change_8_7>0.05 AND &Ratio.7>0.5 then Ind_8_7=1;
else IF &Ratio.7>1 and &Ratio.8<1 then Ind_8_7=1;
else IF &Ratio.8 ne -9 and &Ratio.7 = -9 then Ind_8_7=1;
else IF &Ratio.7>0.5 AND &Ratio.8 in (0,-8,-9) then Ind_8_7=1;
else Ind_8_7=0;
IF PCT_change_9_8>0.05 AND &Ratio.8>0.5 then Ind_9_8=1;
else IF &Ratio.8>1 and &Ratio.9<1 then Ind_9_8=1;
else IF &Ratio.9 ne -9 and &Ratio.8 = -9 then Ind_9_8=1;
else IF &Ratio.8>0.5 AND &Ratio.9 in (0,-8,-9) then Ind_9_8=1;
else Ind_9_8=0;
IF PCT_change_10_9>0.05 AND &Ratio.9>0.5 then Ind_10_9=1;
else IF &Ratio.9>1 and &Ratio.10<1 then Ind_10_9=1;
else IF &Ratio.10 ne -9 and &Ratio.9 = -9 then Ind_10_9=1;
else IF &Ratio.9>0.5 AND &Ratio.10 in (0,-8,-9) then Ind_10_9=1;
else Ind_10_9=0;
IF PCT_change_11_10>0.05 AND &Ratio.10>0.5 then Ind_11_10=1;
else IF &Ratio.10>1 and &Ratio.11<1 then Ind_11_10=1;
else IF &Ratio.11 ne -9 and &Ratio.10 = -9 then Ind_11_10=1;
else IF &Ratio.10>0.5 AND &Ratio.11 in (0,-8,-9) then Ind_11_10=1;
else Ind_11_10=0;
IF PCT_change_12_11>0.05 AND &Ratio.11>0.5 then Ind_12_11=1;
else IF &Ratio.11>1 and &Ratio.12<1 then Ind_12_11=1;
else IF &Ratio.12 ne -9 and &Ratio.11 = -9 then Ind_12_11=1;
else IF &Ratio.11>0.5 AND &Ratio.12 in (0,-8,-9) then Ind_12_11=1;
else Ind_12_11=0;
Run;
/***********Way2-Try to write it in macro code****************/
/***********Way2-Try to write it in macro code****************/
/***********Way2-Try to write it in macro code****************/
/***********Way2-Try to write it in macro code****************/
Data want2;
set have;
%macro RRR(Ratio,from,To);
IF &Ratio.2 not in (0,-6,-7,-8,-9,.) and &Ratio.1 not in (-6,-7,-8,-9,.) then PCT_change_&From._&To.=(&Ratio.1-&Ratio.2)/&Ratio.2;
IF PCT_change_&From._&To.>0.05 AND &Ratio.1>0.5 then Ind_&From._&To.=1;
/****השינוי מחודש קודם גדול מ5% והיחס בחודש יעד גדול מ50%****/
else IF &Ratio.&To.>1 and &Ratio.&From.<1 then Ind_&From._&To.=1;
/***מעבר ממצב של לא חורג למצב של חורג***/
else IF &Ratio.&From. ne -9 and &Ratio.&To. = -9 then Ind_&From._&To.=1;
/***בחודש יעד מנצל לא מסגרת ובחודש קודם במצב אחר****/
else IF &Ratio.&To.>0.5 AND &Ratio.&From. in (0,-8,-9) then Ind_&From._&To.=1;
/***יחס בחודש יעד גדול מ50% ובחודש קודם יחס היה 0 או מינוס 99999,99998***/
else Ind_&From._&To.=0;
%mend RRR;
%RRR(Ratio=RevRatio,from=2,To=1)
%RRR(Ratio=RevRatio,from=3,To=2)
%RRR(Ratio=RevRatio,from=4,To=3)
%RRR(Ratio=RevRatio,from=5,To=4)
%RRR(Ratio=RevRatio,from=6,To=5)
%RRR(Ratio=RevRatio,from=7,To=6)
%RRR(Ratio=RevRatio,from=8,To=7)
%RRR(Ratio=RevRatio,from=9,To=8)
%RRR(Ratio=RevRatio,from=10,To=9)
%RRR(Ratio=RevRatio,from=11,To=10)
%RRR(Ratio=RevRatio,from=12,To=11)
Run;
... View more