From: papu on 20 Jun 2010 19:39 Hi All, Can anyone let me know how to write a perl script or ksh script to change proc format in SAS to if-then-else. Please see below for example. Thanks for your help. /*old file*/ %let _V0 = var1; /* xnbntl75_n */ %let _V1 = var2; /* pct_pay_bal_cyc_1 */ proc format; value V_0_f /* var1 */ 0 -< 1 = "38.6375" 1 -< 2 = "29.8241" 2 -< 3 = "18.8390" 3 -< 4 = "12.8211" 4 -< 5 = "7.8781" 5 -< 8 = "1.4283" 8 - high = "-6.4817" other = "24.2959" ; value V_1_f /* var2 */ 0 -< 3.52 = "10.5909" 3.52 -< 10.87 = "25.4922" 10.87 -< 23.68 = "36.4572" 23.68 -< 99.84 = "37.4047" 99.84 - high = "46.0035" other = "24.2959" ; run; data &SCORE_DS; set &INPUT_DS; /*********************************************************/ /* Note: applying score weight formats to scorecard vars */ /*********************************************************/ cbr_score0 = input ( put( &_V0, V_0_f. ), 8. ); /* var1 */ cbr_score1 = input ( put( round( &_V1, &ROUND_PRECISION ), V_1_f. ), 8. ); /* var2 */; cbr_scaledScore = sum( of cbr_score0-cbr_score1 ); run; endrsubmit; /*New file*/ data &SCORE_DS; set &INPUT_DS; IF 0 <= var1 < 1 THEN cbr_score0 = "38.6375"; ELSE IF 1 <= var1 < 2 THEN cbr_score0 = "29.8241"; ELSE IF 2 <= var1 < 3 THEN cbr_score0 = "18.8390"; ELSE IF 3 <= var1 < 4 THEN cbr_score0 = "12.8211"; ELSE IF 4 <= var1 < 5 THEN cbr_score0 = "7.8781"; ELSE IF 5 <= var1 < 8 THEN cbr_score0 = "1.4283"; ELSE IF 5 <= var1 THEN cbr_score0 = "-6.4817"; ELSE cbr_score0 = "24.2959"; IF 0 <= var2 < 3.52 THEN cbr_score1 = "10.5909"; ELSE IF 3.52 <= var2 < 10.87 THEN cbr_score1 = "25.4922"; ELSE IF 10.87 <= var2 < 23.68 THEN cbr_score1 = "36.4572"; ELSE IF 23.68 <= var2 < 99.84 THEN cbr_score1 = "37.4047"; ELSE IF 99.84 <= var2 THEN cbr_score1 = "46.0035"; ELSE cbr_score1 = "24.2959"; cbr_scaledScore = sum( of cbr_score0-cbr_score1 ); run; endrsubmit;
From: Chris Jones on 21 Jun 2010 05:46 On 21 June, 00:39, papu <prac...(a)gmail.com> wrote: > Hi All, > > Can anyone let me know how to write a perl script or ksh script to > change proc format in SAS to if-then-else. Please see below for > example. Thanks for your help. > > /*old file*/ > > %let _V0 = var1; /* xnbntl75_n */ > %let _V1 = var2; /* pct_pay_bal_cyc_1 */ > > proc format; > value V_0_f /* var1 */ > > 0 -< 1 = "38.6375" > 1 -< 2 = "29.8241" > 2 -< 3 = "18.8390" > 3 -< 4 = "12.8211" > 4 -< 5 = "7.8781" > 5 -< 8 = "1.4283" > 8 - high = "-6.4817" > other = "24.2959" > ; > > value V_1_f /* var2 */ > > 0 -< 3.52 = "10.5909" > 3.52 -< 10.87 = "25.4922" > 10.87 -< 23.68 = "36.4572" > 23.68 -< 99.84 = "37.4047" > 99.84 - high = "46.0035" > other = "24.2959" > ; > run; > > data &SCORE_DS; > set &INPUT_DS; > > /*********************************************************/ > /* Note: applying score weight formats to scorecard vars */ > /*********************************************************/ > cbr_score0 = input ( put( &_V0, V_0_f. ), 8. ); /* var1 */ > cbr_score1 = input ( put( round( &_V1, &ROUND_PRECISION ), > V_1_f. ), 8. ); /* var2 */; > > cbr_scaledScore = sum( of cbr_score0-cbr_score1 ); > run; > endrsubmit; > > /*New file*/ > > data &SCORE_DS; > set &INPUT_DS; > > IF 0 <= var1 < 1 THEN cbr_score0 = "38.6375"; > ELSE IF 1 <= var1 < 2 THEN cbr_score0 = "29.8241"; > ELSE IF 2 <= var1 < 3 THEN cbr_score0 = "18.8390"; > ELSE IF 3 <= var1 < 4 THEN cbr_score0 = "12.8211"; > ELSE IF 4 <= var1 < 5 THEN cbr_score0 = "7.8781"; > ELSE IF 5 <= var1 < 8 THEN cbr_score0 = "1.4283"; > ELSE IF 5 <= var1 THEN cbr_score0 = "-6.4817"; > ELSE cbr_score0 = "24.2959"; > > IF 0 <= var2 < 3.52 THEN cbr_score1 = "10.5909"; > ELSE IF 3.52 <= var2 < 10.87 THEN cbr_score1 = "25.4922"; > ELSE IF 10.87 <= var2 < 23.68 THEN cbr_score1 = "36.4572"; > ELSE IF 23.68 <= var2 < 99.84 THEN cbr_score1 = "37.4047"; > ELSE IF 99.84 <= var2 THEN cbr_score1 = "46.0035"; > ELSE cbr_score1 = "24.2959"; > > cbr_scaledScore = sum( of cbr_score0-cbr_score1 ); > run; > endrsubmit; Why would you want to do such a thing? That's where the power of formats comes in handy... no extensive if-then-else statements.
From: papu on 21 Jun 2010 09:43 It is a work requirement. On Jun 21, 5:46 am, Chris Jones <chris...(a)gmail.com> wrote: > On 21 June, 00:39, papu <prac...(a)gmail.com> wrote: > > > > > > > Hi All, > > > Can anyone let me know how to write a perl script or ksh script to > > change proc format in SAS to if-then-else. Please see below for > > example. Thanks for your help. > > > /*old file*/ > > > %let _V0 = var1; /* xnbntl75_n */ > > %let _V1 = var2; /* pct_pay_bal_cyc_1 */ > > > proc format; > > value V_0_f /* var1 */ > > > 0 -< 1 = "38.6375" > > 1 -< 2 = "29.8241" > > 2 -< 3 = "18.8390" > > 3 -< 4 = "12.8211" > > 4 -< 5 = "7.8781" > > 5 -< 8 = "1.4283" > > 8 - high = "-6.4817" > > other = "24.2959" > > ; > > > value V_1_f /* var2 */ > > > 0 -< 3.52 = "10.5909" > > 3.52 -< 10.87 = "25.4922" > > 10.87 -< 23.68 = "36.4572" > > 23.68 -< 99.84 = "37.4047" > > 99.84 - high = "46.0035" > > other = "24.2959" > > ; > > run; > > > data &SCORE_DS; > > set &INPUT_DS; > > > /*********************************************************/ > > /* Note: applying score weight formats to scorecard vars */ > > /*********************************************************/ > > cbr_score0 = input ( put( &_V0, V_0_f. ), 8. ); /* var1 */ > > cbr_score1 = input ( put( round( &_V1, &ROUND_PRECISION ), > > V_1_f. ), 8. ); /* var2 */; > > > cbr_scaledScore = sum( of cbr_score0-cbr_score1 ); > > run; > > endrsubmit; > > > /*New file*/ > > > data &SCORE_DS; > > set &INPUT_DS; > > > IF 0 <= var1 < 1 THEN cbr_score0 = "38.6375"; > > ELSE IF 1 <= var1 < 2 THEN cbr_score0 = "29.8241"; > > ELSE IF 2 <= var1 < 3 THEN cbr_score0 = "18.8390"; > > ELSE IF 3 <= var1 < 4 THEN cbr_score0 = "12.8211"; > > ELSE IF 4 <= var1 < 5 THEN cbr_score0 = "7.8781"; > > ELSE IF 5 <= var1 < 8 THEN cbr_score0 = "1.4283"; > > ELSE IF 5 <= var1 THEN cbr_score0 = "-6.4817"; > > ELSE cbr_score0 = "24.2959"; > > > IF 0 <= var2 < 3.52 THEN cbr_score1 = "10.5909"; > > ELSE IF 3.52 <= var2 < 10.87 THEN cbr_score1 = "25.4922"; > > ELSE IF 10.87 <= var2 < 23.68 THEN cbr_score1 = "36.4572"; > > ELSE IF 23.68 <= var2 < 99.84 THEN cbr_score1 = "37.4047"; > > ELSE IF 99.84 <= var2 THEN cbr_score1 = "46.0035"; > > ELSE cbr_score1 = "24.2959"; > > > cbr_scaledScore = sum( of cbr_score0-cbr_score1 ); > > run; > > endrsubmit; > > Why would you want to do such a thing? That's where the power of > formats comes in handy... no extensive if-then-else statements.- Hide quoted text - > > - Show quoted text -
From: data _null_; on 21 Jun 2010 11:22 On Jun 21, 8:43 am, papu <prac...(a)gmail.com> wrote: > It is a work requirement. Can you expain the requirement in more detail. As in WHY? You can use the CONTOUT data from PROC FORMAT to fulfill this request. Your format names make generating the variable name that's the object of the IF's pretty easy, but that would not be the case most of the time. This is only a model you must test it to unsure it writes the proper statements. proc format cntlout=control; value V_0_f /* var1 */ 0 -< 1 = "38.6375" 1 -< 2 = "29.8241" 2 -< 3 = "18.8390" 3 -< 4 = "12.8211" 4 -< 5 = "7.8781" 5 -< 8 = "1.4283" 8 - high = "-6.4817" other = "24.2959" ; value V_1_f /* var2 */ 0 -< 3.52 = "10.5909" 3.52 -< 10.87 = "25.4922" 10.87 -< 23.68 = "36.4572" 23.68 -< 99.84 = "37.4047" 99.84 - high = "46.0035" other = "24.2959" ; value V_2_f /* var2 */ low -< 3.52 = "10.5909" 3.52 -< 10.87 = "25.4922" 10.87 -< 23.68 = "36.4572" 23.68 -< 99.84 = "37.4047" 99.84 - high = "46.0035" other = "24.2959" ; run; proc print; run; data _null_; retain small big; if _n_ eq 1 then do; small = -constant('SMALL'); big = constant('BIG'); end; set control; by fmtname notsorted; length vname $32; vname = cats('cbr_score',scan(fmtname,2,'_')); if type eq 'N' then do; if first.fmtname then do; put +3 'select;'; end; if last.fmtname and index(HLO,'O') then do; put +6 'otherwise ' vname ' = ' label ';'; end; else do; put +6 'when(' @; if index(HLO,'L') and start eq 'LOW' then put small :best32. @; else put start @; if sexcl eq 'N' then put ' LE ' @; else put ' LT ' @; put vname @; if eexcl eq 'N' then put ' LE ' @; else put ' LT ' @; if index(HLO,'H') and end eq 'HIGH' then put big :best32. @; else put end @; put ') ' vname ' = ' label ';'; end; if last.fmtname and not indexC(HLO,'O') then put +6 'otherwise;'; if last.fmtname then put +6 'end;'; end; run;
|
Pages: 1 Prev: SAS Technical support Next: Weight and stratum statement |