BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
desireatem
Pyrite | Level 9

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;

1 ACCEPTED SOLUTION

Accepted Solutions
AncaTilea
Pyrite | Level 9

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

9 REPLIES 9
LinusH
Tourmaline | Level 20

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

Data never sleeps
sas_pgmr
Calcite | Level 5
This helped me. Thank you.
Cynthia_sas
SAS Super FREQ

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

data_null__
Jade | Level 19

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

desireatem
Pyrite | Level 9

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;

CTorres
Quartz | Level 8

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,

desireatem
Pyrite | Level 9

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;

Vince28_Statcan
Quartz | Level 8

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.

AncaTilea
Pyrite | Level 9

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 46734 views
  • 3 likes
  • 8 in conversation