<?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 read number of observation by variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589869#M168767</link>
    <description>Yes!!</description>
    <pubDate>Thu, 19 Sep 2019 00:44:34 GMT</pubDate>
    <dc:creator>Hhh111</dc:creator>
    <dc:date>2019-09-19T00:44:34Z</dc:date>
    <item>
      <title>How to read number of observation by variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589718#M168709</link>
      <description>Hi all,&lt;BR /&gt;&lt;BR /&gt;I need opinions on how to read number of observation based on different customer. For example, my data as per below&lt;BR /&gt;&lt;BR /&gt;Dataset A&lt;BR /&gt;Customer| customer no | count&lt;BR /&gt;A | 111 | 3&lt;BR /&gt;B | 222 | 2&lt;BR /&gt;&lt;BR /&gt;Dataset B (Main)&lt;BR /&gt;Customer | customer no | sales&lt;BR /&gt;A | 111 | 12000&lt;BR /&gt;A | 111 | 2000&lt;BR /&gt;A | 111 | 10000&lt;BR /&gt;A | 111 | 7000&lt;BR /&gt;B | 222 | 1000&lt;BR /&gt;B | 222 | 5000&lt;BR /&gt;B | 222 | 6500&lt;BR /&gt;&lt;BR /&gt;In dataset A, the count indicates how many observation i need to extract from dataset B (Main dataset). So i dont know how to randomly select the no of observation by each customer as shown above.&lt;BR /&gt;&lt;BR /&gt;Thanks.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 18 Sep 2019 15:53:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589718#M168709</guid>
      <dc:creator>Hhh111</dc:creator>
      <dc:date>2019-09-18T15:53:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to read number of observation by variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589720#M168710</link>
      <description>&lt;P&gt;So in this case, you want to randomly pick 3 obs from group Customer=A and 2 obs from Customer=B?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2019 15:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589720#M168710</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-09-18T15:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to read number of observation by variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589729#M168715</link>
      <description>&lt;P&gt;The general method would be to assign a random number to each record, then sort by customer number and that random number, then select the first COUNT records of each Customer number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something liek this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data both;
     merge a b;
    by customer_no;
    random_no=rand('uniform');
run;
proc sort data=both;
    by customer_no random_no;
run;
data want;
    set both;
    by customer_no;
    if first.customer_no then seq=0;
    seq+1;
    if seq&amp;lt;=count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Sep 2019 16:37:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589729#M168715</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-18T16:37:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to read number of observation by variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589738#M168717</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45239"&gt;@Hhh111&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way to do it is by sorting sales data in random order for each customer and then keep only the first n observarions from each customer, where n is the individual sample size for each customer:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Get test data 1 - Sample size for each customer;
data sample_no;
	input Customer$ customer_no count;
	cards;
A 111 3
B 222 2
;
run;

* Get test data 2 - Sales data set;
data main;
	input Customer$ customer_no sales;
		cards;
A 111 12000
A 111 2000
A 111 10000
A 111 7000
B 222 1000
B 222 5000
B 222 6500
;
run;

* Add random number to sales data set to use for sorting;
data m2; set main;
	shuffle_id = ranuni(3);
run;

* Join sample size on all records in main and sort on customer - random number;
proc sql;
	create table m3 as 
	select a.Customer, a.customer_no, a.sales, a.shuffle_id, b.count
	from m2 as a inner join sample_no as b
	on a.Customer = b.Customer and a.customer_no = b.customer_no
	order by a.Customer, a.customer_no, a.shuffle_id;
quit;

* Keep only wanted count of records for each customer;
data want (drop=shuffle_id count rcnt);
	set m3;
	retain rcnt;
	by Customer customer_no;
	if first.customer_no then rcnt = 0;
	rcnt = rcnt + 1;
	if rcnt &amp;lt;= count then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Sep 2019 16:57:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589738#M168717</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-09-18T16:57:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to read number of observation by variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589740#M168718</link>
      <description>Oh, didn't see the answer from PaigeMiller. Basically the same, but he came up with a more elegant solution with two steps only!</description>
      <pubDate>Wed, 18 Sep 2019 17:03:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589740#M168718</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-09-18T17:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to read number of observation by variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589747#M168721</link>
      <description>PROC SURVEYSELECT has a sampsize option where you need to provide the sizes as a data set or values. I think you should be using that here instead of doing it manually via a data step.</description>
      <pubDate>Wed, 18 Sep 2019 17:22:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589747#M168721</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-18T17:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to read number of observation by variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589759#M168727</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data A;
input Customer $ customerno count;
datalines;
A 111 3
B 222 2
;

data B;
input Customer $ customerno sales;
datalines;
A 111 12000
A 111 2000
A 111 10000
A 111 7000
B 222 1000
B 222 5000
B 222 6500
;

data want(keep=Customer customerno sales);
	if _N_=1 then do;
		declare hash h();
		h.definekey('p');
		h.definedone();
	end;

	do _N_=1 by 1 until (last.Customer);
		set B;
		by Customer;
	end;

    set A;

	do until (c=count);
		p=rand('integer', 1, _N_);
		if h.check() ne 0 then do;
			n=_iorc_+p;
			set B point=n;
			output;
			c+1;h.ref();
		end;
	end;
	h.clear();
	_iorc_+count+1;
	c=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Sep 2019 18:14:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589759#M168727</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-09-18T18:14:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to read number of observation by variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589802#M168740</link>
      <description>&lt;P&gt;This is the simplest though&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc surveyselect data=B sampsize=A(rename=(count=_NSIZE_)) 
				      out=want(keep=Customer customerno sales) method=srs noprint;
   	strata Customer;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Sep 2019 19:38:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589802#M168740</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-09-18T19:38:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to read number of observation by variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589837#M168758</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This is the simplest though&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc surveyselect data=B sampsize=A(rename=(count=_NSIZE_)) 
				      out=want(keep=Customer customerno sales) method=srs noprint;
   	strata Customer;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;And if the Customer value might repeat then add the Customerno to the strata statement.&lt;/P&gt;
&lt;P&gt;Both data sets need to be sorted by the variables on the strata statement.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2019 21:16:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589837#M168758</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-09-18T21:16:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to read number of observation by variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589869#M168767</link>
      <description>Yes!!</description>
      <pubDate>Thu, 19 Sep 2019 00:44:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589869#M168767</guid>
      <dc:creator>Hhh111</dc:creator>
      <dc:date>2019-09-19T00:44:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to read number of observation by variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589892#M168782</link>
      <description>&lt;P&gt;Thanks All for the response...i get the solution already&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2019 02:47:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-read-number-of-observation-by-variables/m-p/589892#M168782</guid>
      <dc:creator>Hhh111</dc:creator>
      <dc:date>2019-09-19T02:47:32Z</dc:date>
    </item>
  </channel>
</rss>

