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

Hi, I want to generate 100 dataset with 25 random numbers which are collected from origin dataset by survey functions by loop.

 

Here is the Origin dataset:

Obs i x b dummy a12345678910111213141516171819202122232425262728293031323334353637383940
1001017409719999
1002026002609999
1003046879709999
1004031725329999
100501200679999
1006042785249999
100704414689999
1008048292989999
1009020916909999
1010049082279999
1011037534139999
1012032352889999
1013029048459999
1014035395919999
1015033313789999
1016039145079999
1017047259309999
1018033592989999
1019025654739999
1020037191699999
1021016678729999
1022021969359999
1023046025699999
1024011991369999
1025030464349999
1026017056669999
1027026201259999
1028028142009999
1029033007399999
103002760509999
1031030903449999
1032010917139999
1033047494469999
1034047887139999
1035014141769999
1036020766159999
103702683729999
1038024347129999
1039017601489999
1040032482719999

 

Here is the code of select 1 dataset from the origin dataset:

 

proc surveyselect data=ORD

method=pps sampsize=25 out = randomsurvey_PPS;
size b ;

run;

 

Is there a function to generate 100 datasets with survey function?

 

I suppose the code structure should be :

Do i = 1 to 100;

proc surveryselect data = ORD

method = pps

out = dataout i ?

run;

 

Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
unison
Lapis Lazuli | Level 10

Using cars (sampling by weight) as an example:

 

data have;
	set sashelp.cars;
run;

*naive macro approach -- not using REPS option;
%macro loop(dsin=,dsoutpfx=,n=);
	%do i=1 %to &n. %by 1;
		proc surveyselect noprint data=&dsin.
		method=pps sampsize=25 out = &dsoutpfx._&i.;
		size weight;
		run;
	%end;
	proc sql; drop table tempsortsize; quit;
%mend;

%loop(dsin=have,dsoutpfx=smple,n=5);

* using REPS option;
proc surveyselect noprint data=have
method=pps sampsize=25 reps=5 out = samples;
size weight;
run;

*(splitting if necessary);
data _null_;
	do i=1 to 5;
		call execute(
			cats(
				'data smple_',i,'; 
					set samples(where=(replicate=',i,')); 
				 run;'
			)
		);
	end;
run;

-unison

-unison

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

I think you can use the option REPS=100 to obtain the 100 desired samples, they will be in one data set, but you can split them up if necessary (probably not necessary).

--
Paige Miller
unison
Lapis Lazuli | Level 10

Using cars (sampling by weight) as an example:

 

data have;
	set sashelp.cars;
run;

*naive macro approach -- not using REPS option;
%macro loop(dsin=,dsoutpfx=,n=);
	%do i=1 %to &n. %by 1;
		proc surveyselect noprint data=&dsin.
		method=pps sampsize=25 out = &dsoutpfx._&i.;
		size weight;
		run;
	%end;
	proc sql; drop table tempsortsize; quit;
%mend;

%loop(dsin=have,dsoutpfx=smple,n=5);

* using REPS option;
proc surveyselect noprint data=have
method=pps sampsize=25 reps=5 out = samples;
size weight;
run;

*(splitting if necessary);
data _null_;
	do i=1 to 5;
		call execute(
			cats(
				'data smple_',i,'; 
					set samples(where=(replicate=',i,')); 
				 run;'
			)
		);
	end;
run;

-unison

-unison
hashman
Ammonite | Level 13

@Gustavo8:

As suggested by @PaigeMiller and @unison, generate the entire thing using REPS=100. I can't imagine why you would need to split it into 100 chunks but if you really want to do it dynamically in a single step, the hash object is your best friend. Note that below REP=5 is used instead of 100 for the sake of sanity.

data have ;                                                                                                                             
  do i = 1001 to 1050 ;                                                                                                                 
    x = 0 ; b + 2 ; dummy + 1 ;                                                                                                         
    output ;                                                                                                                            
  end ;                                                                                                                                 
run ;                                                                                                                                   
                                                                                                                                        
proc surveyselect noprint data=have out=samples method=pps sampsize=25 reps=5 ;                                                         
  size b ;                                                                                                                              
run ;                                                                                                                                   
                                                                                                                                        
data _null_ ;                                                                                                                           
  if _n_ = 1 then do ;                                                                                                                  
    dcl hash h (dataset:"samples(obs=0)", multidata:"y", ordered:"a") ;                                                                 
    h.definekey ("replicate") ;                                                                                                         
    h.definedata (all:"y") ;                                                                                                            
    h.definedone () ;                                                                                                                   
  end ;                                                                                                                                 
  do until (last.replicate) ;                                                                                                           
    set samples ;                                                                                                                       
    by replicate ;                                                                                                                      
    h.add() ;                                                                                                                           
  end ;                                                                                                                                 
  h.output (dataset: "sample" || put (_n_, z3.)) ;                                                                                      
  h.clear() ;                                                                                                                           
run ;          

The output data set names are auto-created as sample001, sample002, et al. to make them appear properly sorted by name when viewed in the library. You can change that and/or shape the names the way you want by editing the character expression assigned to the DATASET argument tag in the OUTPUT method call.

 

Kind regards

Paul D.

   

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2419 views
  • 4 likes
  • 4 in conversation