<?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 Issue with random number generation and how to create random numbers for subgroups in data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Issue-with-random-number-generation-and-how-to-create-random/m-p/769863#M244146</link>
    <description>&lt;P&gt;Dear SAS experts&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am a relatively new user, but have used Stata for some years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to be able to generate random numbers uniformily distributed. I want to do this seperately for certain subgroups in my dataset, although it might actually suffice to do it for the whole dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data example;&lt;BR /&gt;input name $ value;&lt;BR /&gt;datalines;&lt;BR /&gt;Maria 2&lt;BR /&gt;Maria 3&lt;BR /&gt;Maria 6&lt;BR /&gt;John 2&lt;BR /&gt;John 6&lt;BR /&gt;Peter 2&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to randomly select a 'Maria' observation and a 'John' observation (only 1 Peter, no selection needed) such that I am left with only one of the values of 'value'. To the first issue: I thought it would be easy to generate random data for the whole dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;select count(*) into :N from example;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data example (drop=i);&lt;BR /&gt;set example;&lt;BR /&gt;do i=1 to &amp;amp;N;&lt;BR /&gt;uni_ran=rand("Uniform");&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the amount of observations in the dataset goes from 6 to 36 - why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second issue that I would like to be able to generate random numbers seperately for 'Maria' and 'John'. In general, how would one go about doing this? I have tried by including a -by- but I cannot get it to work. I imagine I have to somehow confine the do loop to the span of observations in each category, e.g. 3 for Maria and 2 for John, perhaps using some functions which identifies the amount of observations in a category of a certain variable. In the actual dataset I am using there are in many cases &amp;gt;3 observations within each category.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general I have an interest in finding out how to perform different operations for each subgroup of a dataset without having to qualify the subgroup using fx -if- and -where', i.e. just "do this for each level of var". If there are 1000 levels of categorical variable this would require many lines of code using -if-.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does someone have a suggestion on how I can resolve the issues mentioned above?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 23 Sep 2021 10:34:24 GMT</pubDate>
    <dc:creator>mgrasmussen</dc:creator>
    <dc:date>2021-09-23T10:34:24Z</dc:date>
    <item>
      <title>Issue with random number generation and how to create random numbers for subgroups in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Issue-with-random-number-generation-and-how-to-create-random/m-p/769863#M244146</link>
      <description>&lt;P&gt;Dear SAS experts&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am a relatively new user, but have used Stata for some years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to be able to generate random numbers uniformily distributed. I want to do this seperately for certain subgroups in my dataset, although it might actually suffice to do it for the whole dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data example;&lt;BR /&gt;input name $ value;&lt;BR /&gt;datalines;&lt;BR /&gt;Maria 2&lt;BR /&gt;Maria 3&lt;BR /&gt;Maria 6&lt;BR /&gt;John 2&lt;BR /&gt;John 6&lt;BR /&gt;Peter 2&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to randomly select a 'Maria' observation and a 'John' observation (only 1 Peter, no selection needed) such that I am left with only one of the values of 'value'. To the first issue: I thought it would be easy to generate random data for the whole dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;select count(*) into :N from example;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data example (drop=i);&lt;BR /&gt;set example;&lt;BR /&gt;do i=1 to &amp;amp;N;&lt;BR /&gt;uni_ran=rand("Uniform");&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the amount of observations in the dataset goes from 6 to 36 - why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second issue that I would like to be able to generate random numbers seperately for 'Maria' and 'John'. In general, how would one go about doing this? I have tried by including a -by- but I cannot get it to work. I imagine I have to somehow confine the do loop to the span of observations in each category, e.g. 3 for Maria and 2 for John, perhaps using some functions which identifies the amount of observations in a category of a certain variable. In the actual dataset I am using there are in many cases &amp;gt;3 observations within each category.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general I have an interest in finding out how to perform different operations for each subgroup of a dataset without having to qualify the subgroup using fx -if- and -where', i.e. just "do this for each level of var". If there are 1000 levels of categorical variable this would require many lines of code using -if-.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does someone have a suggestion on how I can resolve the issues mentioned above?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Sep 2021 10:34:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Issue-with-random-number-generation-and-how-to-create-random/m-p/769863#M244146</guid>
      <dc:creator>mgrasmussen</dc:creator>
      <dc:date>2021-09-23T10:34:24Z</dc:date>
    </item>
    <item>
      <title>Re: Issue with random number generation and how to create random numbers for subgroups in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Issue-with-random-number-generation-and-how-to-create-random/m-p/769866#M244147</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/388382"&gt;@mgrasmussen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Dear SAS experts&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am a relatively new user, but have used Stata for some years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to be able to generate random numbers uniformily distributed. I want to do this seperately for certain subgroups in my dataset, although it might actually suffice to do it for the whole dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data example;&lt;BR /&gt;input name $ value;&lt;BR /&gt;datalines;&lt;BR /&gt;Maria 2&lt;BR /&gt;Maria 3&lt;BR /&gt;Maria 6&lt;BR /&gt;John 2&lt;BR /&gt;John 6&lt;BR /&gt;Peter 2&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to randomly select a 'Maria' observation and a 'John' observation (only 1 Peter, no selection needed) such that I am left with only one of the values of 'value'. To the first issue: I thought it would be easy to generate random data for the whole dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;select count(*) into :N from example;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data example (drop=i);&lt;BR /&gt;set example;&lt;BR /&gt;do i=1 to &amp;amp;N;&lt;BR /&gt;uni_ran=rand("Uniform");&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the amount of observations in the dataset goes from 6 to 36 - why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;amp;n = 6 and you have a loop that goes 1 to &amp;amp;n and each time outputs the record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;The second issue that I would like to be able to generate random numbers seperately for 'Maria' and 'John'. In general, how would one go about doing this? I have tried by including a -by- but I cannot get it to work. I imagine I have to somehow confine the do loop to the span of observations in each category, e.g. 3 for Maria and 2 for John, perhaps using some functions which identifies the amount of observations in a category of a certain variable. In the actual dataset I am using there are in many cases &amp;gt;3 observations within each category.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general I have an interest in finding out how to perform different operations for each subgroup of a dataset without having to qualify the subgroup using fx -if- and -where', i.e. just "do this for each level of var". If there are 1000 levels of categorical variable this would require many lines of code using -if-.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need separate generation of random variables for Maria and John.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
    set have;
    uni_ran=rand("Uniform");
run;
/* Find the max random number for each name */
proc&amp;nbsp;sort&amp;nbsp;data=have1;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;by&amp;nbsp;name&amp;nbsp;uni_ran;
run;
data&amp;nbsp;want;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;set&amp;nbsp;have1;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;by&amp;nbsp;name&amp;nbsp;uni_ran;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;last.name;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to exclude names with only a single record like Peter, then instead of the line &lt;FONT face="courier new,courier"&gt;if last.name;&lt;/FONT&gt; you can use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if last.name and not first.name;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Sep 2021 11:26:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Issue-with-random-number-generation-and-how-to-create-random/m-p/769866#M244147</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-23T11:26:16Z</dc:date>
    </item>
    <item>
      <title>Re: Issue with random number generation and how to create random numbers for subgroups in data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Issue-with-random-number-generation-and-how-to-create-random/m-p/769868#M244149</link>
      <description>&lt;P&gt;Thank you. Appreciate it.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Sep 2021 11:40:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Issue-with-random-number-generation-and-how-to-create-random/m-p/769868#M244149</guid>
      <dc:creator>mgrasmussen</dc:creator>
      <dc:date>2021-09-23T11:40:29Z</dc:date>
    </item>
  </channel>
</rss>

