使用循环在SAS DATA步骤中定义变量

时间:2016-10-13 15:23:36

标签: loops sas

有人知道如何使用某种循环技术压缩这个长SAS代码吗?

DATA CDS; SET CDS; 
retain find131 find132 find133 find134 find135 find136 find137 find138 find139 find140;
if _n_=1
    THEN DO;
    find131 = prxPARSE('/\d\d\d\d\d\d\d\.\d\d/');
    find132 = prxPARSE('/\d\d\d\d\d\d\d\.\d\d/');
    find133 = prxPARSE('/\d\d\d\d\d\d\d\.\d\d/');
    find134 = prxPARSE('/\d\d\d\d\d\d\d\.\d\d/');
    find135 = prxPARSE('/\d\d\d\d\d\d\d\.\d\d/');
    find136 = prxPARSE('/\d\d\d\d\d\d\d\.\d\d/');
    find137 = prxPARSE('/\d\d\d\d\d\d\d\.\d\d/');
    find138 = prxPARSE('/\d\d\d\d\d\d\d\.\d\d/');
    find139 = prxPARSE('/\d\d\d\d\d\d\d\.\d\d/');
    find140 = prxPARSE('/\d\d\d\d\d\d\d\.\d\d/');
    END;

非常感谢你 马可

1 个答案:

答案 0 :(得分:0)

用循环替换每个find#变量系列。此外,您忘记了原始代码块中的run语句。

%macro simplify;

    DATA CDS;
        SET CDS; 
        retain %do i = 131 %to 140; find&i. %end;;
        if _n_=1 THEN DO;
            %do i = 131 %to 140;
                find&i. = prxPARSE('/\d\d\d\d\d\d\d\.\d\d/');
            %end;
        END;
    RUN;

%mend simplify;

%simplify;