Loop through multible data set

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Loop through multible data set

Hallo SAS cummunities

How can I create a code which loop through multiple dataset.

E.g.

I have 5 different dataset.

  • X201201
  • X201202
  • X201203
  • X201204
  • X201205

and I want to take a random sample of 200 cases from each data set.

Therefor I have chosen 'proc surveyselect'.

Every output must have different name like test1, test2 .... test5

Is there any way to do this in some kind of macro?

/Jens


Accepted Solutions
Solution
‎02-16-2015 01:20 PM
PROC Star
Posts: 7,467

Re: Loop through multible data set

or you could do something like:

%macro select(samplesize);

  %do i=1 %to 5;

    proc surveyselect data=x20120&i.

      method=srs

      n=&samplesize.

      seed=&i.

      out=test&i.;

    run;

  %end;

%mend;

%select(200)

View solution in original post


All Replies
Super User
Posts: 19,770

Re: Loop through multible data set

Here's a skeleton, that assumes you know what you want your proc surveyselect code to be.

There are two parameters because you have two things that will change between iterations of your code.

%macro survey(data_set_in, data_set_out);

proc surveyselect data=&data_set_in out=&data_set_out ....(rest of surveyselect code)

;

run;

%mend;

%survey(x201201, test1);

%survey(x201202, test2);

Solution
‎02-16-2015 01:20 PM
PROC Star
Posts: 7,467

Re: Loop through multible data set

or you could do something like:

%macro select(samplesize);

  %do i=1 %to 5;

    proc surveyselect data=x20120&i.

      method=srs

      n=&samplesize.

      seed=&i.

      out=test&i.;

    run;

  %end;

%mend;

%select(200)

Occasional Contributor
Posts: 7

Re: Loop through multible data set

Thank you it work like a charm

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 204 views
  • 0 likes
  • 3 in conversation