Home > database >  How to loop a single observation from a dataset through proc surveyselect?
How to loop a single observation from a dataset through proc surveyselect?

Time:06-01

trying a different approach. I have a single dataset consisting of 10000 observations, 1000 per ID. I would like to run each ID from that dataset through a proc surveyselect function. I would like the first ID to run through the function then loop to the second ID to run through the function. Outputting a dataset for each ID. How can I execute that? If possible.

CodePudding user response:

Three ways:

Option 1: Call execute

Create a distinct list of IDs and run call execute on each one.

proc sql noprint;
    create table all_ids as
        select distinct id
        from have
    ;
quit;

data _null_;
    set all_ids;

    call execute(cat('
        proc surveyselect data=have out=want_', id, ' sampsize=100;',
            ' where id = ', id, ';
        run;')
    );
run;

Option 2: Loop with a macro

Create a distinct macro list of IDs and loop through using a macro.

%macro survey(sampsize=100);
    proc sql noprint;
        select distinct id
        into :all_ids separated by ' '
        from have
        ;
    quit;

    %do i = 1 %to %sysfunc(countw(&all_ids.) );
        %let id = %scan(&all_ids., &i.);

        proc surveyselect data=have out=want_&id. sampsize=&sampsize.;
            where id = &id.;
        run;

    %end;
%mend;
%survey;

Option 3: Strata

This will not get you individual datasets for each ID, but you can easily stratify by ID and get samples.

proc surveyselect data=have out=want sampsize=100;
    strata id;
run;
  • Related