SAS proc sql做循环

时间:2016-10-15 16:28:34

标签: sql loops sas

我目前正致力于信用卡违法项目。需要专家的帮助,看看是否会发生这种情况

所以我有这段代码

data mcmc2;
set work.mcmc;
array delq(1:15) $2. delq_1-delq_15;

do i = 1 to 15;
delq(i) = substr(delq_36_rev, (i),2) ;
put @1 delq(i);
end;

run;

此代码从delq_36_rev

生成15变量

delq_36_rev看起来像xxxxxxxxxxxxxxx(其中0 <= x <= 7) 所以delq_i看起来像xx

我想要做的事情从这里开始。

来自delq_1~delq_15我会根据数字给出分数

(例如,01 = 1分,12 = 2分,23 = 3分)

但我也会将得分完全合并

所以我想写一个像

这样的do循环
do i = 1 to 15
         when delq_i ="70" then score_i=-6 
         when delq_i in ("71","60") then score_i=-5 
         when delq_i in ("72","61","50") then score_i=-4
         when delq_i in ("73","62","51","40") then score_i=-3
         when delq_i in ("74","63","52","41","30") then score_i=-2
         when delq_i in ("76","65","65","64","54","53","43","42","32","31","20","21","10") then score_i=-1
         when delq_i ="00" then score_i=0
         when delq_i in ("01","11","22","33","44","55","66","77" then score_i=1      
         when delq_i ="12" then score_i=2 
         when delq_i ="23" then score_i=3
         when delq_i ="34" then score_i=4 
         when delq_i ="45" then score_i=5
         when delq_i ="56" then score_i=6 
         when delq_i ="67" then score_i=7

 sum(delq_1-delq_15) as delq_score

请帮助!!

2 个答案:

答案 0 :(得分:1)

如果我理解正确,您可以在一个数据步骤中轻松完成所有这些操作,而无需使用宏:

data want;
  set set work.mcmc;
  array delq(15) $2. delq_1-delq_15;
  array score(15);
  do i = 1 to dim(delq);
    delq[i] = substr(delq_36_rev,i,2);
    select(delq[i]);
      when("70") score[i]=-6;
      when("71","60") score[i]=-5;
      /*etc*/
      otherwise call missing(score[i]);
    end;
  end;
  delq_score = sum(of score[*]);
run;

答案 1 :(得分:0)

考虑在宏中使用带有CASE逻辑的SQL更新语句。但首先要添加score_1-score_15列以及 delq 列。然后在宏中,迭代地更新这些得分列,并在最后一个数据步骤中将它们全部添加到score,最后删除15个得分项。

data mcm2;
    set work.mcmc;
    array delq(1:15) $2. delq_1-delq_15;
    array score(1:15) score_1-score_15;

    do i = 1 to 15;
        delq(i) = substr(delq_36_rev, (i),2) ;     * delq_1-delq_15;
        put @1 delq(i);
        score(i) = .;                              * score_1-score_15;
    end;
run;


%macro loopSQL;
    %do i = 1 %to 15;

        proc sql;
            UPDATE mcm2
            SET score_&i = 
             CASE 
                WHEN delq_&i ="70" THEN -6 
                WHEN delq_&i in ("71","60") THEN -5 
                WHEN delq_&i in ("72","61","50") THEN -4
                WHEN delq_&i in ("73","62","51","40") THEN -3
                WHEN delq_&i in ("74","63","52","41","30") THEN -2
                WHEN delq_&i in ("76","65","65","64","54","53","43","42","32","31","20","21","10") THEN -1
                WHEN delq_&i = "00" THEN 0
                WHEN delq_&i in ("01","11","22","33","44","55","66","77") THEN 1      
                WHEN delq_&i ="12" THEN 2 
                WHEN delq_&i ="23" THEN 3
                WHEN delq_&i ="34" THEN 4 
                WHEN delq_&i ="45" THEN 5
                WHEN delq_&i ="56" THEN 6 
                WHEN delq_&i ="67" THEN 7 
                ELSE .
            END;
        quit;

    %end;
%mend loopSQL;

%loopSQL;                                          * run macro;


data work.mcm2;
    set work.mcm2;

    score = sum(of score_1-score_15);   
    drop score_1-score_15;
run;