<?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 select randomly observation from each 'groups' in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-randomly-observation-from-each-groups/m-p/465126#M118610</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37682"&gt;@viollete&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Do I have to use STRATA option in proc surveyselect?&amp;nbsp; there&amp;nbsp; are more patients who had more than two admissions during time period and for each patients I want to select randomly one.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your STRATA would be the ID variable and you tell survey select to select one per strata.&lt;/P&gt;
&lt;P&gt;Here is my take on this problem:&lt;/P&gt;
&lt;PRE&gt;data have;
 input date ddmmyy10.    id  $;
 format date ddmmyy10.;
datalines;
04/05/2012      1
05/07/2012      1
16/07/2012      1
07/12/2013      1
05/08/2012       2
12/10/2012       2
01/05/2012       3
06/05/2012       3
08/06/2012       3
12/10/2012       3
16/11/2012       3
01/01/2013       3
;
run;
proc sort data=have;
by id date;
run;

data firstlast others;
   set have;
   by id;
   if first.id or last.id then output firstlast;
   else output others;
run;

proc surveyselect data=others
     out=othersamp
     sampsize=1
     ;
   strata id;
run;

data want;
   merge firstlast
       othersamp
   ;
   by id date;
run;&lt;/PRE&gt;
&lt;P&gt;Note the use of a data step to provide the example data in a form that others can use. Also the use of a code box for code using the forum {I} icon to paste code so that the message windows do not reformat code and to visually separate code from narrative.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will end up with two additional variables with this that show the sample probability and weight for the other than first or last observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do not come to us when you only have one record because your data only has a single observation in the starting data...&lt;/P&gt;</description>
    <pubDate>Fri, 25 May 2018 15:45:35 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2018-05-25T15:45:35Z</dc:date>
    <item>
      <title>How to select randomly observation from each 'groups'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-randomly-observation-from-each-groups/m-p/464988#M118570</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have hospital admission data for a two year period. Example of data is showed below (i just put the variables that is connected with my question):&lt;/P&gt;
&lt;P&gt;date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;id&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;04/05/2012&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;05/07/2012&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;16/07/2012&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;07/12/2013&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;
&lt;P&gt;05/08/2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;12/10/2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;01/05/2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;06/05/2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;08/06/2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;12/10/2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;16/11/2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;01/01/2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each patient (id) i need to select the first and the last admission during the time period, and i need randomly select another one from the rest. For example, for the ID=1, first admission would be with date 01/05/2012 and the last admission would be 01/01/2013; how can i randomly select one more admission from the rest?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 08:29:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-randomly-observation-from-each-groups/m-p/464988#M118570</guid>
      <dc:creator>viollete</dc:creator>
      <dc:date>2018-05-25T08:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to select randomly observation from each 'groups'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-randomly-observation-from-each-groups/m-p/464997#M118574</link>
      <description>&lt;P&gt;Sort the data, then in a datastep output the first and last, then the remaining run a surveyselct procedure over it:&lt;/P&gt;
&lt;PRE&gt;data firstlast other;
  set have;
  by id;
  if first.id or last.id then output firstlast;
  else output other;
run;
proc surveyselect...;
run;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_surveyselect_sect021.htm" target="_blank"&gt;https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_surveyselect_sect021.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 09:09:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-randomly-observation-from-each-groups/m-p/464997#M118574</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-05-25T09:09:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to select randomly observation from each 'groups'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-randomly-observation-from-each-groups/m-p/465000#M118577</link>
      <description>&lt;P&gt;Do I have to use STRATA option in proc surveyselect?&amp;nbsp; there&amp;nbsp; are more patients who had more than two admissions during time period and for each patients I want to select randomly one.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 10:04:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-randomly-observation-from-each-groups/m-p/465000#M118577</guid>
      <dc:creator>viollete</dc:creator>
      <dc:date>2018-05-25T10:04:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to select randomly observation from each 'groups'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-randomly-observation-from-each-groups/m-p/465002#M118579</link>
      <description>&lt;P&gt;You would really need to explain your issue.&amp;nbsp; Post test data in the form of a datastep, and show what the output should look like.&amp;nbsp; You can where clause the data before taking first and last, that would leave just the visits which are within the window, but not first and last to take a sampling of 1 record per id.&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 10:20:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-randomly-observation-from-each-groups/m-p/465002#M118579</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-05-25T10:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to select randomly observation from each 'groups'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-randomly-observation-from-each-groups/m-p/465126#M118610</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37682"&gt;@viollete&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Do I have to use STRATA option in proc surveyselect?&amp;nbsp; there&amp;nbsp; are more patients who had more than two admissions during time period and for each patients I want to select randomly one.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your STRATA would be the ID variable and you tell survey select to select one per strata.&lt;/P&gt;
&lt;P&gt;Here is my take on this problem:&lt;/P&gt;
&lt;PRE&gt;data have;
 input date ddmmyy10.    id  $;
 format date ddmmyy10.;
datalines;
04/05/2012      1
05/07/2012      1
16/07/2012      1
07/12/2013      1
05/08/2012       2
12/10/2012       2
01/05/2012       3
06/05/2012       3
08/06/2012       3
12/10/2012       3
16/11/2012       3
01/01/2013       3
;
run;
proc sort data=have;
by id date;
run;

data firstlast others;
   set have;
   by id;
   if first.id or last.id then output firstlast;
   else output others;
run;

proc surveyselect data=others
     out=othersamp
     sampsize=1
     ;
   strata id;
run;

data want;
   merge firstlast
       othersamp
   ;
   by id date;
run;&lt;/PRE&gt;
&lt;P&gt;Note the use of a data step to provide the example data in a form that others can use. Also the use of a code box for code using the forum {I} icon to paste code so that the message windows do not reformat code and to visually separate code from narrative.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will end up with two additional variables with this that show the sample probability and weight for the other than first or last observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do not come to us when you only have one record because your data only has a single observation in the starting data...&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 15:45:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-randomly-observation-from-each-groups/m-p/465126#M118610</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-05-25T15:45:35Z</dc:date>
    </item>
  </channel>
</rss>

