<?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 Number of Observations in a  SAS Dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Number-of-Observations-in-a-SAS-Dataset/m-p/260333#M50499</link>
    <description>&lt;P&gt;Hi Friend,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have good SQL knowledge but new&amp;nbsp;SAS learner, I need to count&amp;nbsp;total number of observation from one data set&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am following&amp;nbsp;&amp;nbsp;below steps&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data students;&lt;BR /&gt; input gender score;&lt;BR /&gt; cards;&lt;BR /&gt; 1 48&lt;BR /&gt; 1 45&lt;BR /&gt; 2 50&lt;BR /&gt; 2 42&lt;BR /&gt; 1 41&lt;BR /&gt; 2 51&lt;BR /&gt; 1 52&lt;BR /&gt; 1 43&lt;BR /&gt; 2 52&lt;BR /&gt; ;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;now I need&amp;nbsp;total count from above table.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select count(*) from students1;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;above queyr will give output 9 but when I am converting ths into SAS code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data students1;&lt;BR /&gt; set students;&lt;BR /&gt; count + 1;&lt;BR /&gt; by gender;&lt;BR /&gt; if first.gender then count = 1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc print data = students1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;above code I find out from the google and also this is not giving output 9. I am on a SAS learning stage . Please help&lt;/P&gt;
&lt;P&gt;Can you please help me to correct the code and get the count 9&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 31 Mar 2016 07:26:41 GMT</pubDate>
    <dc:creator>CG1</dc:creator>
    <dc:date>2016-03-31T07:26:41Z</dc:date>
    <item>
      <title>Number of Observations in a  SAS Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-Observations-in-a-SAS-Dataset/m-p/260333#M50499</link>
      <description>&lt;P&gt;Hi Friend,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have good SQL knowledge but new&amp;nbsp;SAS learner, I need to count&amp;nbsp;total number of observation from one data set&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am following&amp;nbsp;&amp;nbsp;below steps&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data students;&lt;BR /&gt; input gender score;&lt;BR /&gt; cards;&lt;BR /&gt; 1 48&lt;BR /&gt; 1 45&lt;BR /&gt; 2 50&lt;BR /&gt; 2 42&lt;BR /&gt; 1 41&lt;BR /&gt; 2 51&lt;BR /&gt; 1 52&lt;BR /&gt; 1 43&lt;BR /&gt; 2 52&lt;BR /&gt; ;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;now I need&amp;nbsp;total count from above table.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select count(*) from students1;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;above queyr will give output 9 but when I am converting ths into SAS code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data students1;&lt;BR /&gt; set students;&lt;BR /&gt; count + 1;&lt;BR /&gt; by gender;&lt;BR /&gt; if first.gender then count = 1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;proc print data = students1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;above code I find out from the google and also this is not giving output 9. I am on a SAS learning stage . Please help&lt;/P&gt;
&lt;P&gt;Can you please help me to correct the code and get the count 9&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Mar 2016 07:26:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-Observations-in-a-SAS-Dataset/m-p/260333#M50499</guid>
      <dc:creator>CG1</dc:creator>
      <dc:date>2016-03-31T07:26:41Z</dc:date>
    </item>
    <item>
      <title>Re: Number of Observations in a  SAS Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-Observations-in-a-SAS-Dataset/m-p/260336#M50501</link>
      <description>&lt;P&gt;First of all, this happens:&lt;/P&gt;
&lt;PRE&gt;31         data students1;
32         set students;
33         count + 1;
34         by gender;
35         if first.gender then count = 1;
36         run;

ERROR: BY variables are not properly sorted on data set WORK.STUDENTS.&lt;/PRE&gt;
&lt;P&gt;So you have to do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=students;
by gender;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;before you try that data step.&lt;/P&gt;
&lt;P&gt;Now you get that output:&lt;/P&gt;
&lt;PRE&gt;                                                  Obs    gender    score    count

                                                   1        1        48       1  
                                                   2        1        45       2  
                                                   3        1        41       3  
                                                   4        1        52       4  
                                                   5        1        43       5  
                                                   6        2        50       1  
                                                   7        2        42       2  
                                                   8        2        51       3  
                                                   9        2        52       4  
&lt;/PRE&gt;
&lt;P&gt;You can see that the final counts for both values of gender add up to 9.&lt;/P&gt;
&lt;P&gt;To get a simple total count, do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data students1;
set students;
count + 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, you can get the total number of observations in a dataset much easier (and quicker, once the datasets grow) by querying its metadata&amp;amp;colon;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select nobs from dictionary.tables where libname = 'WORK' and memname = 'STUDENTS';
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 31 Mar 2016 07:43:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-Observations-in-a-SAS-Dataset/m-p/260336#M50501</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-03-31T07:43:32Z</dc:date>
    </item>
    <item>
      <title>Re: Number of Observations in a  SAS Dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-of-Observations-in-a-SAS-Dataset/m-p/260369#M50516</link>
      <description>&lt;P&gt;Although you can count the number of obs using a data step you probably shouldn't. There are many ther ways to do so, one being querying the metadata tables and another is using a summary proc such as proc means or proc freq.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc means data=students;&lt;/P&gt;
&lt;P&gt;class gender;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=students;&lt;/P&gt;
&lt;P&gt;tables gender;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Mar 2016 10:52:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-of-Observations-in-a-SAS-Dataset/m-p/260369#M50516</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-31T10:52:02Z</dc:date>
    </item>
  </channel>
</rss>

