简体   繁体   中英

Defining variables in SAS DATA step with a loop

Does anybody knows how to compress this long SAS code with some sort of looping technique?

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;

Thank you very much
Marco

Replace each series of find# variables with a loop. Also, you forgot a run statement in your original code block.

%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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM