<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Sampling with replacement - different methods, different results? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sampling-with-replacement-different-methods-different-results/m-p/867758#M342732</link>
    <description>&lt;P&gt;To get the same answers, you must use the same random sample. In the PROC SURVEYSELECT calls, you should delete the RANUNI option. The RANUNI option uses an old 1970s-style random number generator (RNG) instead of the modern RNG that is used by the RAND function. Similarly, you should replace the RANUNI call in the second DATA step (the one from&amp;nbsp;psu.edu) with a call to RAND. If you do that, you will get the same random sample and, consequently, the same median, in each case:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create dataset to sample from*/
data ds;
	do i= 1 to 20000;
		x = rand("T", 1);
		output;
	end;
	drop i;
run;

/* 1. Surveyselect + reps*/
proc surveyselect data = ds  sampsize = 1000 
     reps = 10 
     seed = 12345 method = urs 
     out = sample1 (keep = Replicate x:) noprint outhits;
run;

proc means data = sample1 median;
	var x;
	output out = med_1 (drop = _TYPE_ _FREQ_) median= median_svy1;
run;

/* 2. Surveyselect without reps*/
proc surveyselect data = ds  sampsize = 10000 
     seed = 12345 method = urs out = sample2 (keep = Replicate x:) 
     noprint outhits;
run;

proc means data = sample2 median;
	var x;
	output out = med_2 (drop = _TYPE_ _FREQ_) median= median_svy2;
run;


proc iml;
	use ds;
  	read all into ds_mat;
	call randseed(12345);
	s = sample(ds_mat, 10000)`; 
	create sample3 var {x};
	append from s;
	close sample3 ds;
quit;
	
proc means data = sample3 median;
	var x;
	output out = med_3 (drop = _TYPE_ _FREQ_) median= median_iml;
run;



/* a. https://blogs.sas.com/content/iml/2014/01/29/sample-with-replacement-in-sas.html*/
sasfile ds load;
data sample4(drop=i);
call streaminit(12345);
do i = 1 to 10000;         
   p = ceil(NObs * rand("Uniform"));
   set ds nobs=NObs point=p;
   output;
end;
STOP;
run;
sasfile ds close;

proc means data = sample4 median;
	var x;
	output out = med_4 (drop = _TYPE_ _FREQ_) median= median_ds1;
run;


/* b. https://online.stat.psu.edu/stat482/book/export/html/660*/
data sample5;
   call streaminit(12345);
	choose=int(rand("Uniform")*n)+1;
	set ds point=choose nobs=n;
	i+1;
	if i &amp;gt; 10000 then stop;
run;

proc means data = sample5 median;
	var x;
	output out = med_5 (drop = _TYPE_ _FREQ_) median= median_ds2;
run;

data medians;
	merge med_:;
run;

proc print data = medians;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;gt;&amp;nbsp;&lt;EM&gt;Which result is correct? &lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;All results are equally correct. You are asking for the median of a random sample. If you change the random sample, you will get a different median.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;gt; &lt;EM&gt;I'm trying to replicate in SAS some analysis done in R that includes sampling and I get completely different results&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;I don't know what you mean by "completely different results."&amp;nbsp; You will get different results when you use different software because the RNGs are different even if you use the same seed. Also, as KSharp mentions, there will be minor differences due to different default definitions for the quantiles. But you shouldn't be getting "completely different" results. The results should be similar, and should all be within a few standard errors of 0, which is the median for the population.&lt;/P&gt;</description>
    <pubDate>Mon, 03 Apr 2023 13:17:35 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2023-04-03T13:17:35Z</dc:date>
    <item>
      <title>Sampling with replacement - different methods, different results?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sampling-with-replacement-different-methods-different-results/m-p/867730#M342724</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to select a random sample of 10 thousand obs. with replacement from a given dataset (with 20 thousand obs). First, I try different approaches in SAS and compare the medians. I try 5 different approaches (code below), 3 of them give one median value, 2 give another median value. Why is there a difference? Shouldn't the medians be the same given the relatively large sample?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create dataset to sample from*/&lt;BR /&gt;data ds;
	do i= 1 to 20000;
		x = rand("T", 1);
		output;
	end;
	drop i;
run;

/* 1. Surveyselect + reps*/
proc surveyselect data = ds ranuni sampsize = 1000 reps = 10 seed = 12345 method = urs out = sample1 (keep = Replicate x:) noprint outhits;
run;

proc means data = sample1 noprint;
	var x;
	output out = med_1 (drop = _TYPE_ _FREQ_) median= median_svy1;
run;

/* 2. Surveyselect without reps*/
proc surveyselect data = ds ranuni sampsize = 10000 seed = 12345 method = urs out = sample2 (keep = Replicate x:) noprint outhits;
run;

proc means data = sample2 noprint;
	var x;
	output out = med_2 (drop = _TYPE_ _FREQ_) median= median_svy2;
run;

/* 3. IML*/

proc iml;
	use ds;
  	read all into ds_mat;
	call randseed(12345);
	s = sample(ds_mat, 10000)`; 
	create sample3 var {x};
	append from s;
	close sample3 ds;
	
proc means data = sample3 noprint;
	var x;
	output out = med_3 (drop = _TYPE_ _FREQ_) median= median_iml;
run;

/* 4. Data step*/

/* a. https://blogs.sas.com/content/iml/2014/01/29/sample-with-replacement-in-sas.html*/
sasfile ds load;
data sample4(drop=i);
call streaminit(12345);
do i = 1 to 10000;         
   p = ceil(NObs * rand("Uniform"));
   set ds nobs=NObs point=p;
   output;
end;
STOP;
run;
sasfile ds close;

proc means data = sample4 noprint;
	var x;
	output out = med_4 (drop = _TYPE_ _FREQ_) median= median_ds1;
run;

/* b. https://online.stat.psu.edu/stat482/book/export/html/660*/
data sample5;
	choose=int(ranuni(12345)*n)+1;
	set ds point=choose nobs=n;
	i+1;
	if i &amp;gt; 10000 then stop;
run;

proc means data = sample5 noprint;
	var x;
	output out = med_5 (drop = _TYPE_ _FREQ_) median= median_ds2;
run;

data medians;
	merge med_:;
run;

proc print data = medians;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've also tried to do the same in R (using sample function) and Python (using random.choices) and I got even different results for median:&lt;BR /&gt;R: 0.009&lt;BR /&gt;Python: -0.02848&lt;/P&gt;
&lt;P&gt;So, I'm totally confused. Which result is correct? The background for my question is that I'm trying to replicate in SAS some analysis done in R that includes sampling and I get completely different results, so I want to understand the differences.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2023 11:46:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sampling-with-replacement-different-methods-different-results/m-p/867730#M342724</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2023-04-03T11:46:29Z</dc:date>
    </item>
    <item>
      <title>Re: Sampling with replacement - different methods, different results?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sampling-with-replacement-different-methods-different-results/m-p/867751#M342729</link>
      <description>I think there are different definitions(or algorithm ) for median . Like quantiles there are 9 different method to calculate it.&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt; wrote a blog about it before. and I think Rick could give you more details .&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2017/05/24/definitions-sample-quantiles.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2017/05/24/definitions-sample-quantiles.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://blogs.sas.com/content/iml/2021/07/26/compare-quantiles-sas-r-python.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2021/07/26/compare-quantiles-sas-r-python.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 03 Apr 2023 12:53:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sampling-with-replacement-different-methods-different-results/m-p/867751#M342729</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-04-03T12:53:37Z</dc:date>
    </item>
    <item>
      <title>Re: Sampling with replacement - different methods, different results?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sampling-with-replacement-different-methods-different-results/m-p/867757#M342731</link>
      <description>&lt;P&gt;Rule #1 for trying to figure out what is happening ... &lt;STRONG&gt;LOOK AT&lt;/STRONG&gt; the data with your own eyes. If you simply look at SAMPLE1 and SAMPLE3, you will see there are differences between the values of X that are generated.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2023 13:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sampling-with-replacement-different-methods-different-results/m-p/867757#M342731</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-04-03T13:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: Sampling with replacement - different methods, different results?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sampling-with-replacement-different-methods-different-results/m-p/867758#M342732</link>
      <description>&lt;P&gt;To get the same answers, you must use the same random sample. In the PROC SURVEYSELECT calls, you should delete the RANUNI option. The RANUNI option uses an old 1970s-style random number generator (RNG) instead of the modern RNG that is used by the RAND function. Similarly, you should replace the RANUNI call in the second DATA step (the one from&amp;nbsp;psu.edu) with a call to RAND. If you do that, you will get the same random sample and, consequently, the same median, in each case:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create dataset to sample from*/
data ds;
	do i= 1 to 20000;
		x = rand("T", 1);
		output;
	end;
	drop i;
run;

/* 1. Surveyselect + reps*/
proc surveyselect data = ds  sampsize = 1000 
     reps = 10 
     seed = 12345 method = urs 
     out = sample1 (keep = Replicate x:) noprint outhits;
run;

proc means data = sample1 median;
	var x;
	output out = med_1 (drop = _TYPE_ _FREQ_) median= median_svy1;
run;

/* 2. Surveyselect without reps*/
proc surveyselect data = ds  sampsize = 10000 
     seed = 12345 method = urs out = sample2 (keep = Replicate x:) 
     noprint outhits;
run;

proc means data = sample2 median;
	var x;
	output out = med_2 (drop = _TYPE_ _FREQ_) median= median_svy2;
run;


proc iml;
	use ds;
  	read all into ds_mat;
	call randseed(12345);
	s = sample(ds_mat, 10000)`; 
	create sample3 var {x};
	append from s;
	close sample3 ds;
quit;
	
proc means data = sample3 median;
	var x;
	output out = med_3 (drop = _TYPE_ _FREQ_) median= median_iml;
run;



/* a. https://blogs.sas.com/content/iml/2014/01/29/sample-with-replacement-in-sas.html*/
sasfile ds load;
data sample4(drop=i);
call streaminit(12345);
do i = 1 to 10000;         
   p = ceil(NObs * rand("Uniform"));
   set ds nobs=NObs point=p;
   output;
end;
STOP;
run;
sasfile ds close;

proc means data = sample4 median;
	var x;
	output out = med_4 (drop = _TYPE_ _FREQ_) median= median_ds1;
run;


/* b. https://online.stat.psu.edu/stat482/book/export/html/660*/
data sample5;
   call streaminit(12345);
	choose=int(rand("Uniform")*n)+1;
	set ds point=choose nobs=n;
	i+1;
	if i &amp;gt; 10000 then stop;
run;

proc means data = sample5 median;
	var x;
	output out = med_5 (drop = _TYPE_ _FREQ_) median= median_ds2;
run;

data medians;
	merge med_:;
run;

proc print data = medians;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;gt;&amp;nbsp;&lt;EM&gt;Which result is correct? &lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;All results are equally correct. You are asking for the median of a random sample. If you change the random sample, you will get a different median.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;gt; &lt;EM&gt;I'm trying to replicate in SAS some analysis done in R that includes sampling and I get completely different results&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;I don't know what you mean by "completely different results."&amp;nbsp; You will get different results when you use different software because the RNGs are different even if you use the same seed. Also, as KSharp mentions, there will be minor differences due to different default definitions for the quantiles. But you shouldn't be getting "completely different" results. The results should be similar, and should all be within a few standard errors of 0, which is the median for the population.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2023 13:17:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sampling-with-replacement-different-methods-different-results/m-p/867758#M342732</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2023-04-03T13:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: Sampling with replacement - different methods, different results?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sampling-with-replacement-different-methods-different-results/m-p/867765#M342733</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp; for suggestions!&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2023 13:52:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sampling-with-replacement-different-methods-different-results/m-p/867765#M342733</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2023-04-03T13:52:33Z</dc:date>
    </item>
  </channel>
</rss>

