<?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: Create Table with number of observartions and mean in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510289#M6502</link>
    <description>Are you familiar with SQL? That’s the fastest option in this case, due to the multiple distinct counts you’d like.</description>
    <pubDate>Sun, 04 Nov 2018 23:00:12 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-11-04T23:00:12Z</dc:date>
    <item>
      <title>Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510277#M6501</link>
      <description>&lt;P&gt;Hi experts,&lt;/P&gt;&lt;P&gt;my data sample (shortcut) looks like this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Stock&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Country&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Analyst&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Broker&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Date&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;TP2P&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;BCE&lt;/TD&gt;&lt;TD&gt;CA&lt;/TD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;&lt;TD&gt;US&lt;/TD&gt;&lt;TD&gt;Jon&lt;/TD&gt;&lt;TD&gt;BNY&lt;/TD&gt;&lt;TD&gt;10/25/2016&lt;/TD&gt;&lt;TD&gt;0.01&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;7&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;APPLE&lt;/TD&gt;&lt;TD&gt;US&lt;/TD&gt;&lt;TD&gt;Julia&lt;/TD&gt;&lt;TD&gt;BNY&lt;/TD&gt;&lt;TD&gt;07/21/2017&lt;/TD&gt;&lt;TD&gt;0.09&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;8&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;APPLE&lt;/TD&gt;&lt;TD&gt;US&lt;/TD&gt;&lt;TD&gt;Sarah&lt;/TD&gt;&lt;TD&gt;UBS&lt;/TD&gt;&lt;TD&gt;07/30/2018&lt;/TD&gt;&lt;TD&gt;0.10&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to build a table that provides the number of STOCKS, ANALYSTS and BROKER,&lt;BR /&gt;and the means of key variables &lt;STRONG&gt;by country&lt;/STRONG&gt;.&lt;BR /&gt;The resulting table should be like this:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Panel A:&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Country&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;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lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and another table (Panel B) with the following result:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Panel B:&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Year&lt;/TD&gt;&lt;TD&gt;# Obs.&lt;/TD&gt;&lt;TD&gt;#Analysts&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2015&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2016&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2017&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;MEAN&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1.75&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Nov 2018 21:23:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510277#M6501</guid>
      <dc:creator>jozuleta</dc:creator>
      <dc:date>2018-11-04T21:23:53Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510289#M6502</link>
      <description>Are you familiar with SQL? That’s the fastest option in this case, due to the multiple distinct counts you’d like.</description>
      <pubDate>Sun, 04 Nov 2018 23:00:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510289#M6502</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-04T23:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510290#M6503</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;a little bit. If you have a solution using SQL I would appreciate!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 04 Nov 2018 23:22:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510290#M6503</guid>
      <dc:creator>jozuleta</dc:creator>
      <dc:date>2018-11-04T23:22:29Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510298#M6504</link>
      <description>&lt;P&gt;Check this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm=',';
input Stock :$8. Country :$8. Analyst :$8. Broker :$8. Date :$8.TP2P;
datalines;
BCE,CA,Marc,RBC,06/30/2015,0.24
BCE,CA,Marc,RBC,01/17/2017,0.32
ATCO,CA,Marc,RBC,05/23/2017,-0.15
Inditex,ES,Jorge,CAJA,01/16/2018,0.03
IBM,US,Jon,BNY,05/30/2018,0.09
IBM,US,Jon,BNY,10/25/2016,0.01
APPLE,US,Julia,BNY,07/21/2017,0.09
APPLE,US,Sarah,UBS,07/30/2018,0.10
;
run;


proc sql;
create table pre_want as
select 	monotonic() as row_no,
		Country, 
		count(distinct stock) format=4.2 as Stock, 
		count(distinct Analyst) format=4.2 as Analyst,
		count(distinct Broker) format=4.2 as Broker,
		count(*) format=4.2 as Obs,
		mean(TP2P) format=4.2 as mean_TP2P
	from have
group by Country
;
quit;

proc sql;
create table want(drop=row_no) as 
select * from pre_want
union
select monotonic()+10 as row_number,
		'TOTAL' as Country,
		sum(Stock) format=4.2 as Stock,
		sum(Analyst) format=4.2 as Analyst,
		sum(Broker) format=4.2 as Broker,
		sum(Obs) format=4.2as Obs,
		. format=4.2 as mean_TP2P
	from pre_want
union
select monotonic()+100 as row_number,
		'Mean' as Country,
		mean(Stock) format=4.2 as Stock,
		mean(Analyst) format=4.2 as Analyst,
		mean(Broker) format=4.2 as Broker,
		mean(Obs) format=4.2 as Obs,
		mean(mean_TP2P) format=4.2 as mean_TP2P
	from pre_want
order by 1
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Nov 2018 01:20:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510298#M6504</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-11-05T01:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510299#M6505</link>
      <description>Are the data for each state mutually exclusive? Can you assume that you can add up the # of analysts or would that need to be  separate count distinct()?&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 05 Nov 2018 01:38:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510299#M6505</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-05T01:38:36Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510302#M6506</link>
      <description>count distinct. The code from SuryaKiran works good. Thanks to you, too Reeza!</description>
      <pubDate>Mon, 05 Nov 2018 02:20:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510302#M6506</guid>
      <dc:creator>jozuleta</dc:creator>
      <dc:date>2018-11-05T02:20:54Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510303#M6507</link>
      <description>The solution does not do a count distinct for totals. So if you have overlaps, this will not be correct. &lt;BR /&gt;</description>
      <pubDate>Mon, 05 Nov 2018 02:30:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510303#M6507</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-05T02:30:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510309#M6508</link>
      <description>Do you mind to reply your solution to see better the differences in the codes / data ? And, What about Panel B (by years). Any help you could provide? Thanks</description>
      <pubDate>Mon, 05 Nov 2018 03:12:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510309#M6508</guid>
      <dc:creator>jozuleta</dc:creator>
      <dc:date>2018-11-05T03:12:43Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510312#M6509</link>
      <description>You should be able to adapt the code already given. For years, change the GROUP to include YEAR not STATE. For totals hit the main table again but consider what would go into the GROUP statement. You already have all the pieces, just need to reorder them a bit. &lt;BR /&gt;Also, see what happens if you have more than 10 states or years. &lt;BR /&gt;&lt;BR /&gt;If you're having issues post the code you've modified and we can let you know what you need to do.</description>
      <pubDate>Mon, 05 Nov 2018 03:26:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/510312#M6509</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-05T03:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511568#M6540</link>
      <description>&lt;P&gt;Hi Reeza,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I thought it would be that easy as you mentioned but unfortunately it doesn't work &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;Here the code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table MyData.PRE_Table1 as
select 	monotonic() as row_no,
		year format=BESTw. length=8, 
		count(distinct RIC) format=8.0 as RIC, 
		count(distinct ANCODE) format=8.0 as ANCODE,
		count(distinct BROKERNAM) format=8.0 as BROKERNAM,
		count(*) format=8.0 as Obs,
		mean(TP2P) format=8.2 as mean_TP2P
	from MyData.SAMP_ESTIMATOR
group by year 
;
quit;
proc sql;
create table MyData.Tabelle_1(drop=row_no) as 
select * from MyData.PRE_Table1
union
select monotonic()+10 as row_number,
		'MEAN' as year,
		sum(RIC) format=8.0 as RIC,
		sum(ANCODE) format=8.0 as ANCODE,
		sum(BROKERNAM) format=8.0 as BROKERNAM,
		sum(Obs) format=8.0 as Obs,
		. format=8.2 as mean_TP2P
	from MyData.PRE_Table1
union
select monotonic()+100 as row_number,
		'MEAN' as year,
		mean(RIC) format=8.0 as RIC,
		mean(ANCODE) format=8.0 as ANCODE,
		mean(BROKERNAM) format=8.0 as BROKERNAM,
		mean(Obs) format=8.0 as Obs,
		mean(mean_TP2P) format=8.2 as mean_TP2P
	from MyData.PRE_Table1
order by 1
;
quit;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The first table is created without any problems. But then in the second proc I get this error:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV class="sasError"&gt;ERROR: Column 2 from the first contributor of UNION is not the same type as its counterpart from the second.&lt;/DIV&gt;&lt;DIV class="sasError"&gt;ERROR: Column 2 from the first contributor of UNION is not the same type as its counterpart from the second.&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;I do not see exactly why it should not be the same type ?!&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;Could you provide some help again? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;Here is a screenshot of the Table (PRE_Table1) having also the information about the variable "year"&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Unbenannt.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/24784iC73A4925498F4ECB/image-size/large?v=v2&amp;amp;px=999" role="button" title="Unbenannt.PNG" alt="Unbenannt.PNG" /&gt;&lt;/span&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 09 Nov 2018 01:24:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511568#M6540</guid>
      <dc:creator>jozuleta</dc:creator>
      <dc:date>2018-11-09T01:24:31Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511574#M6541</link>
      <description>Year is numeric in your first data set and Total is clearly not numeric, it's a character variable. &lt;BR /&gt;&lt;BR /&gt;You can change Year to be a character value (Put(year, 4.)) or you can Mean/Total to be a number and use a format to display it as mean/total. Changing Year to be a character is easier. But...if you ever need to sort this for some reason, then the number method is easier though you do need to have a format. I usually just set the number to something arbitrarily large (100000 = Mean, 100001 = Total).</description>
      <pubDate>Fri, 09 Nov 2018 01:32:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511574#M6541</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-09T01:32:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511619#M6542</link>
      <description>&lt;P&gt;Sorry, I guess I do not know exactly how to change it in my code.&lt;/P&gt;&lt;P&gt;I&amp;nbsp;changed it like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table JORGE.PRE_Table1 as
select 	monotonic() as row_no,
		year (Put(year, 4.)), 
		count(distinct RIC) format=8.0 as RIC, 
		count(distinct ANCODE) format=8.0 as ANCODE,
		count(distinct BROKERNAM) format=8.0 as BROKERNAM,
		count(*) format=8.0 as Obs,
		mean(TP2P) format=8.2 as mean_TP2P
	from JORGE.SAMP_ESTIMATOR
group by year
;
quit;
proc sql;
create table JORGE.Tabelle_1(drop=row_no) as 
select * from JORGE.PRE_Table1
union
select monotonic()+10 as row_number,
		'TOTAL' as year,
		sum(RIC) format=8.0 as RIC,
		sum(ANCODE) format=8.0 as ANCODE,
		sum(BROKERNAM) format=8.0 as BROKERNAM,
		sum(Obs) format=8.0 as Obs,
		. format=8.2 as mean_TP2P
	from JORGE.PRE_Table1
union
select monotonic()+100 as row_number,
		'MEAN' as year,
		mean(RIC) format=8.0 as RIC,
		mean(ANCODE) format=8.0 as ANCODE,
		mean(BROKERNAM) format=8.0 as BROKERNAM,
		mean(Obs) format=8.0 as Obs,
		mean(mean_TP2P) format=8.2 as mean_TP2P
	from JORGE.PRE_Table1
order by 1
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;...and I get now the following error:&lt;/P&gt;&lt;DIV class="sasError"&gt;&lt;EM&gt;&amp;nbsp;ERROR: Function YEAR requires a numeric expression as argument 1.&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;U&gt;Furthermore&lt;/U&gt;, I checked the means between the different tables (&lt;FONT color="#0000FF"&gt;&lt;EM&gt;Reference table&lt;/EM&gt;&lt;/FONT&gt; &lt;STRONG&gt;vs&lt;/STRONG&gt; &lt;FONT color="#339966"&gt;&lt;EM&gt;table ordered by country&lt;/EM&gt;&lt;/FONT&gt; &lt;STRONG&gt;vs&lt;/STRONG&gt; &lt;FONT color="#FF0000"&gt;&lt;EM&gt;table ordered by year&lt;/EM&gt;&lt;/FONT&gt;) for the TP2P variable.They are all different...which means that there is something wrong with the first code.&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#0000FF"&gt;&lt;EM&gt;- Reference table (SAMP_ESTIMATOR)&lt;/EM&gt;&lt;/FONT&gt;: mean of 0.2191538 (using proc means)&lt;/DIV&gt;&lt;DIV class="sasError"&gt;- &lt;FONT color="#339966"&gt;&lt;EM&gt;Table ordered by country (&lt;FONT color="#008000"&gt;Pre_Table1&lt;/FONT&gt;)&lt;/EM&gt;&lt;/FONT&gt;: 0.1918582 (using proc means)&lt;/DIV&gt;&lt;DIV class="sasError"&gt;- &lt;FONT color="#FF0000"&gt;&lt;EM&gt;Table ordered by year (Pre_Table2): &lt;/EM&gt;&lt;/FONT&gt;0.2271409 (using proc means)&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;The means should be all the same as the mean of the &lt;FONT color="#0000FF"&gt;&lt;EM&gt;Reference table&lt;/EM&gt;&lt;/FONT&gt;: &lt;STRONG&gt;0.2191538&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;Conclusion: There is something wrong with the whole code &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;I will provide you with the two tables, maybe it helps to solve the problem easier (the reference table is too large to upload).&lt;/DIV&gt;&lt;DIV class="sasError"&gt;- &lt;FONT color="#008000"&gt;Pre_Table1 is ordered by country&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;- &lt;FONT color="#FF0000"&gt;Pre_Table2 is ordered by year&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;Thanks!&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasNote"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Fri, 09 Nov 2018 11:15:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511619#M6542</guid>
      <dc:creator>jozuleta</dc:creator>
      <dc:date>2018-11-09T11:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511664#M6543</link>
      <description>&lt;P&gt;YEAR() function argument to be numeric.&amp;nbsp; Remove put() function inside year()&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token function"&gt;year&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;Put&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;year&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;4&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;) ---&amp;gt; year(year)&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perform the calculations on the right grouped columns&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Nov 2018 13:24:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511664#M6543</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-11-09T13:24:25Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511693#M6544</link>
      <description>&lt;P&gt;Hi SuryaKiran,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately it doesn't work. Now I have a new column called "_TEMA007" instead of "year". Furthermore, the years are all set now to "1995".&lt;/P&gt;&lt;P&gt;my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table JORGE.PRE_Table2 as
select 	monotonic() as row_no,
		year(year), 
		count(distinct RIC) format=8.0 as RIC, 
		count(distinct ANCODE) format=8.0 as ANCODE,
		count(distinct BROKERNAM) format=8.0 as BROKERNAM,
		count(distinct COMPLOC) fortmat=8.0 as COMPLOC,
		count(*) format=8.0 as Obs,
		mean(TP2P) format=8.2 as mean_TP2P
	from JORGE.SAMP_ESTIMATOR
group by year
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Even If I a run the whole code&amp;nbsp; the problems is still there:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="sasError"&gt;ERROR: Column 2 from the first contributor of UNION is not the same type as its counterpart from the second.&lt;/DIV&gt;&lt;DIV class="sasError"&gt;ERROR: Column 2 from the first contributor of UNION is not the same type as its counterpart from the second.&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#000000"&gt;Do you have an idea why the means are different from my reference table ? They shouldn't. &lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;&lt;FONT color="#000000"&gt;The Total of Observations are the same but apparently the means are not?! (even when changing the format of the numers to eg. 8.8)&lt;BR /&gt;&lt;/FONT&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 09 Nov 2018 14:29:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511693#M6544</guid>
      <dc:creator>jozuleta</dc:creator>
      <dc:date>2018-11-09T14:29:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511718#M6547</link>
      <description>&lt;P&gt;Do you have year column in&amp;nbsp;JORGE.SAMP_ESTIMATOR dataset as numeric? then use&amp;nbsp;&lt;STRONG&gt;put(year,4.) as year&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When your appending two tables using UNION then you need to have same data types for same columns. In your case my guess is your first query has year numeric and the next queries are character.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Nov 2018 15:43:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511718#M6547</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-11-09T15:43:59Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511721#M6548</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/236579"&gt;@jozuleta&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Sorry, I guess I do not know exactly how to change it in my code.&lt;/P&gt;
&lt;P&gt;I&amp;nbsp;changed it like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table JORGE.PRE_Table1 as
select 	monotonic() as row_no,
		Put(year, 4.) as Year, 
		count(distinct RIC) format=8.0 as RIC, 
		count(distinct ANCODE) format=8.0 as ANCODE,
		count(distinct BROKERNAM) format=8.0 as BROKERNAM,
		count(*) format=8.0 as Obs,
		mean(TP2P) format=8.2 as mean_TP2P
	from JORGE.SAMP_ESTIMATOR
group by year
;
quit;
proc sql;
create table JORGE.Tabelle_1(drop=row_no) as 
select * from JORGE.PRE_Table1
union
select monotonic()+10 as row_number,
		'TOTAL' as year,
		sum(RIC) format=8.0 as RIC,
		sum(ANCODE) format=8.0 as ANCODE,
		sum(BROKERNAM) format=8.0 as BROKERNAM,
		sum(Obs) format=8.0 as Obs,
		. format=8.2 as mean_TP2P
	from JORGE.PRE_Table1
union
select monotonic()+100 as row_number,
		'MEAN' as year,
		mean(RIC) format=8.0 as RIC,
		mean(ANCODE) format=8.0 as ANCODE,
		mean(BROKERNAM) format=8.0 as BROKERNAM,
		mean(Obs) format=8.0 as Obs,
		mean(mean_TP2P) format=8.2 as mean_TP2P
	from JORGE.PRE_Table1

;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="sasNote"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="sasNote"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;BLOCKQUOTE&gt;
&lt;DIV class="sasError"&gt;The means should be all the same as the mean of the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT color="#0000FF"&gt;&lt;EM&gt;Reference table&lt;/EM&gt;&lt;/FONT&gt;:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;0.2191538&lt;/STRONG&gt;&lt;/DIV&gt;
&lt;DIV class="sasError"&gt;Conclusion: There is something wrong with the whole code &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;DIV class="sasError"&gt;Why would you expect the means to be the same?&amp;nbsp; They should not be the same otherwise what's the point of having the different groupings. The overall means should be the same, but if you haven any missing values again, they may not be.&lt;/DIV&gt;
&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="sasError"&gt;See the above modification and let us know.&lt;/DIV&gt;
&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="sasError"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Fri, 09 Nov 2018 15:55:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511721#M6548</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-09T15:55:47Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511738#M6550</link>
      <description>&lt;P&gt;Guys thank you very much. It worked.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;Yes, for the groups they should not be the same (as you said) BUT&amp;nbsp;the OVERALL MEANS should be the same (as you said, too). But unfortunately they are not the same.&lt;/P&gt;&lt;P&gt;If a run a proc means for all the three tables the means are different as described below. See screenshot&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 332px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/24804iFBEB22C0CFCF2923/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Why is this the reason and how can I solve it? I can not report tables with different overall means &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;&lt;P&gt;First proc mean is from the Reference Sample, second is from the Table by countries, third is from the Table by years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for helping!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Nov 2018 16:55:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511738#M6550</guid>
      <dc:creator>jozuleta</dc:creator>
      <dc:date>2018-11-09T16:55:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511742#M6551</link>
      <description>Look at your N's, why aren't they the same? &lt;BR /&gt;</description>
      <pubDate>Fri, 09 Nov 2018 16:57:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511742#M6551</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-09T16:57:52Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511747#M6552</link>
      <description>They are not the same because one table is ordered by country (with N=63) and the other by years (with 18). But the overall mean should anyway be the same ?! I checked the overall observations in each table and they are for all the same (all the Tables consider 1574783 obs). I am starting to think that I am getting stupid ^^</description>
      <pubDate>Fri, 09 Nov 2018 17:23:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511747#M6552</guid>
      <dc:creator>jozuleta</dc:creator>
      <dc:date>2018-11-09T17:23:50Z</dc:date>
    </item>
    <item>
      <title>Re: Create Table with number of observartions and mean</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511758#M6553</link>
      <description>I am definitely getting stupid and should do a break now &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt; Sorry for asking... of course they are not the same... the mean of means is different.... sorry...hahahah &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;</description>
      <pubDate>Fri, 09 Nov 2018 17:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Create-Table-with-number-of-observartions-and-mean/m-p/511758#M6553</guid>
      <dc:creator>jozuleta</dc:creator>
      <dc:date>2018-11-09T17:49:57Z</dc:date>
    </item>
  </channel>
</rss>

