ERROR: The %DO statement is not valid in open code.

Accepted Solution Solved
Reply
Super Contributor
Posts: 292
Accepted Solution

ERROR: The %DO statement is not valid in open code.

Hello, How can I fix this by maintaining  %do u=1 %to 20;  .........%end

data new;

set new;

run;

%do u=1 %to 20; 

proc surveyselect data=new  method = urs sampsize = 141 OUTHITS

     reps=1 seed=1234100 out=new_&u;

run;

ERROR: The %DO statement is not valid in open code.

CODES..........

%end;


Accepted Solutions
Solution
‎01-16-2014 03:09 PM
Super Contributor
Posts: 543

Re: ERROR: The %DO statement is not valid in open code.

Do you do something in the CODES... part that modifies the output data sets?

when I ran your code like this, it worked:

%MACRO DO_BRANCH;

%do u=1 %to 4;

proc surveyselect data=SASHELP.CLASS  method = urs sampsize = 141 OUTHITS

     reps=1 seed=1234100 out=new_&u;

run;

/*CODES.....*/

%end;

%MEND DO_BRANCH;

%DO_BRANCH;

View solution in original post


All Replies
Esteemed Advisor
Posts: 5,198

Re: ERROR: The %DO statement is not valid in open code.

By having it in closed code (that is, wrap it into a macro definition).

Data never sleeps
Learner
Posts: 1

Re: ERROR: The %DO statement is not valid in open code.

This helped me. Thank you.
SAS Super FREQ
Posts: 8,719

Re: ERROR: The %DO statement is not valid in open code.

Hi, for a longer explanation of what LinusH posted, please see my paper "Macro Basics for New SAS Users". If you are struggling with the correct usage for %DO, it is possible that the paper will explain that in more detail and, the rest of the paper might be useful too.

http://support.sas.com/resources/papers/proceedings13/120-2013.pdf

Cynthia

Respected Advisor
Posts: 3,775

Re: ERROR: The %DO statement is not valid in open code.

SURVERSELECT has features that should eliminate the need to MACRO look it.

Super Contributor
Posts: 292

Re: ERROR: The %DO statement is not valid in open code.

I put it in a macro it runs but no data is created. The data set test is real data that I want to do random sample with replacement. I can not use the option in proc surveyselec that does not require macro because of some computations that were performed in the code

data test;

set test;

run;

%MACRO DO_BRANCH;

%do u=1 %to 20;

proc surveyselect data=test  method = urs sampsize = 141 OUTHITS

     reps=1 seed=1234100 out=new_&u;

run;

ERROR: The %DO statement is not valid in open code.

CODES..........

%end;

%MEND DO_BRANCH;

Regular Contributor
Posts: 180

Re: ERROR: The %DO statement is not valid in open code.

I think what you need to do is:

%MACRO DO_BRANCH;

%do u=1 %to 20;

proc surveyselect data=test  method = urs sampsize = 141 OUTHITS

     reps=1 seed=1234100 out=new_&u;

run;

CODES.....

%end;

%MEND DO_BRANCH;

%DO_BRANCH

Regards,

Super Contributor
Posts: 292

Re: ERROR: The %DO statement is not valid in open code.

Thanks; I ran the code but surveyselect created just new_1 and no data from new_2 to new_20

I ran the code:

%MACRO DO_BRANCH;

%do u=1 %to 20;

proc surveyselect data=test  method = urs sampsize = 141 OUTHITS

     reps=1 seed=1234100 out=new_&u;

run;

CODES.....

%end;

%MEND DO_BRANCH;

%DO_BRANCH;

Super Contributor
Posts: 339

Re: ERROR: The %DO statement is not valid in open code.

The following code works just fine when testing

%MACRO DO_BRANCH;

%do u=1 %to 20;

proc surveyselect data=sashelp.class  method = urs sampsize = 14 OUTHITS

     reps=1 seed=1234100 out=new_&u;

run;

%end;

%MEND DO_BRANCH;

%DO_BRANCH;

However, 2 important things to note. Since all procedure resets to the start of the seed, your current implementation would yield new_1=new_2=...=new_20. That is, the exact same 141 records would be selected.

I suspect its the part in CODES... that triggers your empty new_2 to new_20

As DN pointed out, a more natural approach would be to use REPS=20 and then split the resulting file BY REPLICATE (the automatically generated variable listing the count of REPS). This would clear your issue with having to change seeds each time as then it keeps on drawing numbers from the same seed rather than reseting at the beginning of the seed for each procedure iteration.

Solution
‎01-16-2014 03:09 PM
Super Contributor
Posts: 543

Re: ERROR: The %DO statement is not valid in open code.

Do you do something in the CODES... part that modifies the output data sets?

when I ran your code like this, it worked:

%MACRO DO_BRANCH;

%do u=1 %to 4;

proc surveyselect data=SASHELP.CLASS  method = urs sampsize = 141 OUTHITS

     reps=1 seed=1234100 out=new_&u;

run;

/*CODES.....*/

%end;

%MEND DO_BRANCH;

%DO_BRANCH;

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 8715 views
  • 2 likes
  • 8 in conversation