<?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-Assigning Random Values Based on Specific Criteria to Another Value Within the Same Variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Re-Assigning-Random-Values-Based-on-Specific-Criteria-to-Another/m-p/954069#M372669</link>
    <description>&lt;P&gt;I would like to randomly re-assign records based on criteria. For example in the table below, I would like to re-assign 30% of records where the ID is 4 to be an ID of 2, and 10% of the records where the ID is 3 to be an ID of 2. I have looked into the rand function using both bernoulli and uniform and can't seem to get it right.&lt;/P&gt;&lt;P&gt;I believe using the rand("uniform") or rand("bernoulli") functions is what would work best, but I can't seem to get the code dynamic enough to account for the specific percentages and also re-assign based on two different criteria.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="chsprogramming_0-1734547705457.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103179i15BA6B6A59059705/image-size/medium?v=v2&amp;amp;px=400" role="button" title="chsprogramming_0-1734547705457.png" alt="chsprogramming_0-1734547705457.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 18 Dec 2024 18:49:11 GMT</pubDate>
    <dc:creator>chsprogramming</dc:creator>
    <dc:date>2024-12-18T18:49:11Z</dc:date>
    <item>
      <title>Re-Assigning Random Values Based on Specific Criteria to Another Value Within the Same Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Re-Assigning-Random-Values-Based-on-Specific-Criteria-to-Another/m-p/954069#M372669</link>
      <description>&lt;P&gt;I would like to randomly re-assign records based on criteria. For example in the table below, I would like to re-assign 30% of records where the ID is 4 to be an ID of 2, and 10% of the records where the ID is 3 to be an ID of 2. I have looked into the rand function using both bernoulli and uniform and can't seem to get it right.&lt;/P&gt;&lt;P&gt;I believe using the rand("uniform") or rand("bernoulli") functions is what would work best, but I can't seem to get the code dynamic enough to account for the specific percentages and also re-assign based on two different criteria.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="chsprogramming_0-1734547705457.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103179i15BA6B6A59059705/image-size/medium?v=v2&amp;amp;px=400" role="button" title="chsprogramming_0-1734547705457.png" alt="chsprogramming_0-1734547705457.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2024 18:49:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Re-Assigning-Random-Values-Based-on-Specific-Criteria-to-Another/m-p/954069#M372669</guid>
      <dc:creator>chsprogramming</dc:creator>
      <dc:date>2024-12-18T18:49:11Z</dc:date>
    </item>
    <item>
      <title>Re: Re-Assigning Random Values Based on Specific Criteria to Another Value Within the Same Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Re-Assigning-Random-Values-Based-on-Specific-Criteria-to-Another/m-p/954075#M372671</link>
      <description>&lt;P&gt;Something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if id=4 and rand('uniform')&amp;lt;0.3 then id=2;
else if id=3 and rand('uniform')&amp;lt;0.1 then id=2;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Dec 2024 19:16:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Re-Assigning-Random-Values-Based-on-Specific-Criteria-to-Another/m-p/954075#M372671</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-12-18T19:16:42Z</dc:date>
    </item>
    <item>
      <title>Re: Re-Assigning Random Values Based on Specific Criteria to Another Value Within the Same Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Re-Assigning-Random-Values-Based-on-Specific-Criteria-to-Another/m-p/954076#M372672</link>
      <description>&lt;P&gt;I would suggest using Proc SURVEYselect to select the ones for reassignment:&lt;/P&gt;
&lt;P&gt;Unfortunately you provided "example" as a picture, not a working data step or even text.&lt;/P&gt;
&lt;P&gt;The ID would be treated as a STRATA variable so for the procedure to work the data would have to be sorted by the id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
   input id count;
   /* create one observation per from summary for example data*/
   do i=1 to count;
      output;
   end;
   drop i count;
datalines;
1 3
2 2
3 8
4 10
;

proc surveyselect data=have out=selected  outall
    samprate =(100 100 10 30);
  ;
  strata id;
  id id;
run;&lt;/PRE&gt;
&lt;P&gt;The output data set has an added variable "selected" that can be used in a data step to do the adjustment. One way:&lt;/P&gt;
&lt;PRE&gt;data want;
   set selected;
   select (id);
      when (3) if selected=1 then id= 2;
      when (4) if selected=1 then id= 2;
      otherwise;
   end;
   drop selected  selectionprob samplingweight;
run;&lt;/PRE&gt;
&lt;P&gt;I am using a Select/When block instead of if/then/else as I am suspecting this process may get pushed to more than a couple of values of "Id" and the select is easier to keep straight in that case. Since your example has both assigning a value of 2&lt;/P&gt;
&lt;PRE&gt;   when (3,4) if selected=1 then id=2;&lt;/PRE&gt;
&lt;P&gt;could be used. The other way&amp;nbsp; would be more flexible for different assignments for different Id values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You said 10% of the 3s. But only showed 4 values. So I'm not quite sure what you expect. The SAMPRATE is a percentage. You can use SAMPSIZE to specify a specific number. The SAMPRATE or SAMPSIZE should have one entry for each Strata when the rate/counts may different. Don't mix the size or rate, pick one. If you want all of the values for a strata then the rate is 100 (or 1) or size the number of observations for the id.&lt;/P&gt;
&lt;P&gt;With small numbers it is likely that the "rate" approach may not select any. 10% of 4 is 0.4 which would be the expected number of selected ID values of 3. Which means most likely not going to get any.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2024 21:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Re-Assigning-Random-Values-Based-on-Specific-Criteria-to-Another/m-p/954076#M372672</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-12-18T21:30:09Z</dc:date>
    </item>
    <item>
      <title>Re: Re-Assigning Random Values Based on Specific Criteria to Another Value Within the Same Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Re-Assigning-Random-Values-Based-on-Specific-Criteria-to-Another/m-p/954077#M372673</link>
      <description>&lt;P&gt;Depends on what you mean.&lt;/P&gt;
&lt;P&gt;If you just want to give each observation that has ID=4 a 30% CHANCE of being reassigned do something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  if id=4 and rand('uniform')&amp;lt;=0.30 then id=2;
  else if id=3 and rand('uniform')&amp;lt;=0.10 then id=2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But since the choice on each is random (pseudorandom) it might be that 20% or 40% of them get transformed instead of 30%.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to instead say that since there are 4 observations with ID=3 that you want only 1 of them transformed into ID=2 you will need to do something different.&amp;nbsp; In that case calculate how many you want transformed.&amp;nbsp; The randomly order the observations and change the first N.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have;
  tables id / noprint out=counts;
run;
data counts;
  set counts;
  if id=4 then count=ceil(count*0.30);
  else if id=3 then count=ceil(count*0.10);
  keep id count;
run;
data random;
  set have;
  randnum = rand('uniform');
run;
proc sort data=random;
  by id randnum;
run;
data want;
   merge random counts;
   by id;
   newid = id;
   if count&amp;gt;0 then if id in (3,4) then newid=2;
   output;
   count+(-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Dec 2024 19:19:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Re-Assigning-Random-Values-Based-on-Specific-Criteria-to-Another/m-p/954077#M372673</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-12-18T19:19:24Z</dc:date>
    </item>
  </channel>
</rss>

