<?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: How to randomly delete three observations by group? in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508860#M15775</link>
    <description>The randomly deleting part words well. The final output I want is to add a new column, which is the result after randomly deleting 3 obs, to the original dataset. SuryaKiran's code works well, which randomly selected 3 obs and then merge them as missing value to the original one.</description>
    <pubDate>Tue, 30 Oct 2018 18:18:06 GMT</pubDate>
    <dc:creator>hua</dc:creator>
    <dc:date>2018-10-30T18:18:06Z</dc:date>
    <item>
      <title>How to randomly delete three observations by group?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508803#M15765</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to randomly delete three observations&amp;nbsp;for each group, and add the result as a new column. Does anyone know how to randomly delete three observations by group?&lt;/P&gt;
&lt;P&gt;The dataset I have is like the following:&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;year&amp;nbsp; &amp;nbsp; &amp;nbsp;prep&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;550&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;600&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;580&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2003&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;550&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2004&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;570&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;550&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;600&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 2001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;600&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 2002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;580&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 2003&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;550&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;2004&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;570&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;and I'm trying to get the new dataset as:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;year&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;prep&amp;nbsp; &amp;nbsp; &amp;nbsp;new&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;550&amp;nbsp; &amp;nbsp; &amp;nbsp; 550&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;600&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;580&amp;nbsp; &amp;nbsp; &amp;nbsp; 580&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2003&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;550&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2004&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;570&amp;nbsp; &amp;nbsp; &amp;nbsp; 570&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2005&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;530&amp;nbsp; &amp;nbsp; &amp;nbsp; 530&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2006&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;545&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;550&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;550&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 2000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;600&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;600&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 2001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;600&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 2002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;580&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 2003&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;550&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;550&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;2004&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;570&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;570&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;...&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you all!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Best,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Hua&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 16:31:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508803#M15765</guid>
      <dc:creator>hua</dc:creator>
      <dc:date>2018-10-30T16:31:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to randomly delete three observations by group?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508813#M15766</link>
      <description>1. Add a random number&lt;BR /&gt;2. sort by random number&lt;BR /&gt;3. Set to missing for the first 3 (or reassign as needed).&lt;BR /&gt;4. Resort to desired order&lt;BR /&gt;&lt;BR /&gt;I don't know of another quick way, except possibly using PROC SURVEYSELECT and select N-3 from each group and then merging that back in.</description>
      <pubDate>Tue, 30 Oct 2018 16:45:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508813#M15766</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-30T16:45:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to randomly delete three observations by group?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508821#M15767</link>
      <description>&lt;P&gt;Some idea here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID     year     prep;
cards;
1      2000       550
1      2001       600
1      2002       580
1      2003       550
1      2004       570
1      2018       550
2      2000       600
2      2001       600
2      2002       580
2      2003       550
2      2004       570
;
data want;
do _n_=1 by 1 until(last.id);
set have;
by id;
end;
array t(3) _temporary_;
do _d=1 to 3;
t(_d)=rand('integer',1,_n_);
end;
do _n_=1 by 1 until(last.id);
set have;
by id;
if _n_ not in t then output;
end;
drop _:;
run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Oct 2018 16:54:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508821#M15767</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-30T16:54:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to randomly delete three observations by group?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508822#M15768</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/55231"&gt;@hua&lt;/a&gt;&amp;nbsp; This version is&amp;nbsp; a better one :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
array t(3) _temporary_;
call missing(of t(*), __d);
do _n_=1 by 1 until(last.id);
set have;
by id;
end;
do while(n(of t(*))&amp;lt;dim(t));
_d=rand('integer',1,_n_);
if _d not in t then do;__d+1; t(__d)=_d;end;
end;
do _n_=1 by 1 until(last.id);
set have;
by id;
if _n_ not in t then output;
end;
drop _:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Oct 2018 17:08:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508822#M15768</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-30T17:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to randomly delete three observations by group?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508828#M15769</link>
      <description>&lt;P&gt;I recommend using PROC SURVEYSELECT. Seed values changes with time so that when ever you run the random changes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
prep=500;
do id=1 to 3;
	do year=2000 to 2008;
	prep+50;
	output;
	end;
end;
run;

proc surveyselect data=have
	method=srs n=3 
	seed=%SYSFUNC(round(%Sysfunc(time())))  
	out=test (Keep=id year prep) noprint ;
strata id ;
run;

proc sql;
create table want as 
select a.*,case when b.prep is null then a.prep else . end as new_prep
	from have a
	left join test b on (a.id=b.id and a.year=b.year);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Oct 2018 17:22:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508828#M15769</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-10-30T17:22:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to randomly delete three observations by group?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508830#M15770</link>
      <description>&lt;P&gt;That can be over come by simple assignment of&amp;nbsp;&lt;/P&gt;
&lt;P&gt;call streaminit(123);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
call streaminit(123);
array t(3) _temporary_;
call missing(of t(*), __d);
do _n_=1 by 1 until(last.id);
set have;
by id;
end;
do while(n(of t(*))&amp;lt;dim(t));
_d=rand('integer',1,_n_);
if _d not in t then do;__d+1; t(__d)=_d;end;
end;
do _n_=1 by 1 until(last.id);
set have;
by id;
if _n_ not in t then output;
end;
drop _:;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I bet this this much faster if the requirement is "randomly delete three observations by group"&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 17:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508830#M15770</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-30T17:25:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to randomly delete three observations by group?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508857#M15772</link>
      <description>Thanks! It's a good way to do it!</description>
      <pubDate>Tue, 30 Oct 2018 18:10:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508857#M15772</guid>
      <dc:creator>hua</dc:creator>
      <dc:date>2018-10-30T18:10:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to randomly delete three observations by group?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508858#M15773</link>
      <description>Yes, it works for randomly deleting 3 obs, all I need to do next is to merge the result to the original dataset. Thank you!</description>
      <pubDate>Tue, 30 Oct 2018 18:11:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508858#M15773</guid>
      <dc:creator>hua</dc:creator>
      <dc:date>2018-10-30T18:11:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to randomly delete three observations by group?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508859#M15774</link>
      <description>&lt;P&gt;Hmm Hang on I think I m missing something, There should not be a need merge back. I admit I haven't got the clarity of final output you want. Can you clarify, let me tweak the existing one plz&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think this change will do&lt;/P&gt;
&lt;P&gt;if _n_ in t then call missing(new);&lt;BR /&gt;else new=prep;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
call streaminit(123);
array t(3) _temporary_;
call missing(of t(*), __d);
do _n_=1 by 1 until(last.id);
set have;
by id;
end;
do while(n(of t(*))&amp;lt;dim(t));
_d=rand('integer',1,_n_);
if _d not in t then do;__d+1; t(__d)=_d;end;
end;
do _n_=1 by 1 until(last.id);
set have;
by id;
if _n_  in t then call missing(new);
else new=prep;
output;
end;
drop _:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Oct 2018 18:18:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508859#M15774</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-30T18:18:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to randomly delete three observations by group?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508860#M15775</link>
      <description>The randomly deleting part words well. The final output I want is to add a new column, which is the result after randomly deleting 3 obs, to the original dataset. SuryaKiran's code works well, which randomly selected 3 obs and then merge them as missing value to the original one.</description>
      <pubDate>Tue, 30 Oct 2018 18:18:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508860#M15775</guid>
      <dc:creator>hua</dc:creator>
      <dc:date>2018-10-30T18:18:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to randomly delete three observations by group?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508861#M15776</link>
      <description>&lt;P&gt;Got it. Sure proc survey select is convenient and i tested that being much slower. I have modified mine for my satisfaction after all.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 18:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-randomly-delete-three-observations-by-group/m-p/508861#M15776</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-30T18:21:18Z</dc:date>
    </item>
  </channel>
</rss>

