<?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: Count no. of obs from multiple datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606891#M176312</link>
    <description>&lt;P&gt;Querying the view sashelp.vtable might be faster than counting the observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select nobs
    into :odsnumber trimmed
    from sashelp.vtable
      where libname = 'WORK' and memname = "FKEYS&amp;amp;i."
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also note, that if executed in a loop, the varaible odsnumber will contain the number of observations of the last iteration only.&lt;/P&gt;</description>
    <pubDate>Mon, 25 Nov 2019 08:41:31 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2019-11-25T08:41:31Z</dc:date>
    <item>
      <title>Count no. of obs from multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606885#M176309</link>
      <description>&lt;P&gt;I'm trying to write the code to count the number of observations from multiple datasets as below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm creating the datasets as below and I want to sum&amp;nbsp;the number of observations from all the datasets which was created in the previous step and I want that value to display in the macro variable 'obsunmber'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to know whether the proc&amp;nbsp;sql step (second step)&amp;nbsp;which I wrote below will accompoish the task which I wanted to. I'm unable to test the below code at the moment as well due to server outage.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/******create all the rows from the foreign key table if there are rows which has not match the primary key************/
proc sql noprint;
	create table fkeys&amp;amp;i. as select a.* from &amp;amp;_INPUT. as a left outer join &amp;amp;libname..&amp;amp;dsnname. as b 
		on a.&amp;amp;fieldname.=b.&amp;amp;fieldname1.
	where b.&amp;amp;fieldname1. IS NULL;
quit;

/********* Retrieve the number of observations in the dataset ***************/

proc sql noprint;
	select count(*) into: obsnumber from fkeys&amp;amp;i.;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Nov 2019 07:35:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606885#M176309</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2019-11-25T07:35:08Z</dc:date>
    </item>
    <item>
      <title>Re: Count no. of obs from multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606887#M176310</link>
      <description>&lt;P&gt;The proc Sql step you have written will do as you require. You could also achieve with a data step with out reading the data (using compile time option nobs)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; Call symputx('onsnumber',numobs);&lt;/P&gt;&lt;P&gt;&amp;nbsp; Stop;&lt;/P&gt;&lt;P&gt;&amp;nbsp; Set fkeys&amp;amp;I nobs=numobs;&lt;/P&gt;&lt;P&gt;Run;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 07:46:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606887#M176310</guid>
      <dc:creator>Lucy1</dc:creator>
      <dc:date>2019-11-25T07:46:07Z</dc:date>
    </item>
    <item>
      <title>Re: Count no. of obs from multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606891#M176312</link>
      <description>&lt;P&gt;Querying the view sashelp.vtable might be faster than counting the observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select nobs
    into :odsnumber trimmed
    from sashelp.vtable
      where libname = 'WORK' and memname = "FKEYS&amp;amp;i."
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also note, that if executed in a loop, the varaible odsnumber will contain the number of observations of the last iteration only.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 08:41:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606891#M176312</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-11-25T08:41:31Z</dc:date>
    </item>
    <item>
      <title>Re: Count no. of obs from multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606892#M176313</link>
      <description>&lt;P&gt;SQL automatically creates a macro variable, SQLOBS, which contains the number of observations from the last statement. So you can just use&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let obsnumber=&amp;amp;sqlobs;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;right after your query.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 08:42:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606892#M176313</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-11-25T08:42:07Z</dc:date>
    </item>
    <item>
      <title>Re: Count no. of obs from multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606893#M176314</link>
      <description>&lt;P&gt;But I want to see the number of observations from all the iterations not only the last one. It would be good if I see the sum of observations from all the iterations.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 08:46:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606893#M176314</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2019-11-25T08:46:52Z</dc:date>
    </item>
    <item>
      <title>Re: Count no. of obs from multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606896#M176316</link>
      <description>&lt;P&gt;I ran the code below and I got 'odsnumber' value as 19 and what I except is 20.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;

%let i=1;

data test&amp;amp;i.;
set sashelp.class(obs=1);
run;

%let i=2;

data test&amp;amp;i.;
set sashelp.class;
run;

proc sql noprint;
  select nobs
    into :odsnumber trimmed
    from sashelp.vtable
      where libname = 'WORK' and memname = "TEST&amp;amp;i."
  ;
quit;

%put &amp;amp;odsnumber.;

%mend;
%test;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Nov 2019 09:04:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606896#M176316</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2019-11-25T09:04:18Z</dc:date>
    </item>
    <item>
      <title>Re: Count no. of obs from multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606898#M176317</link>
      <description>&lt;P&gt;As in your question, if you want to get the total number of observations then you can achieve this by keeping a running total.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, the following code creates data sets and then keeps a total of the number of observations created, which can be seen in the log:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let total_nobs = 0;

%macro total_nobs;
   %do i = 1 %to 3;

      data class&amp;amp;i;
         set sashelp.class;
      run;

      data _null_;
         call symputx('total_nobs', sum(&amp;amp;total_nobs,nobs));
         set class&amp;amp;i(obs = 0) nobs = nobs;
      run;

   %end;
%mend total_nobs;

options mprint;
%total_nobs;

%put total_nobs = &amp;amp;total_nobs;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 09:13:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606898#M176317</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2019-11-25T09:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: Count no. of obs from multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606899#M176318</link>
      <description>&lt;P&gt;Macro variable references, like &amp;amp;i, will not automatically reference all values of i - only the latest value you gave it (i.e. 1 OR 2 OR 3, not 1 then 2 then 3) so although your first code was correct you will need to use the macro language to loop round all values of i and keep a tally. Amir has provided a good solution for the tallying aspect of your question&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 09:18:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606899#M176318</guid>
      <dc:creator>Lucy1</dc:creator>
      <dc:date>2019-11-25T09:18:51Z</dc:date>
    </item>
    <item>
      <title>Re: Count no. of obs from multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606927#M176335</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I want to see the number of observations from all the iterations not only the last one&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Then you should add &amp;amp;SQLOBS to your running total inside your macro, e.g.:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Initialize the total before calling the macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%global obsnumber;
%let obsnumber=0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And inside the macro, after the SQL statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let obsnumber=%eval(&amp;amp;obsnumber+&amp;amp;sqlobs);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Just make sure that you do not have a %LOCAL macro variable of the same name, then that will be updated instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 11:19:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606927#M176335</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-11-25T11:19:12Z</dc:date>
    </item>
    <item>
      <title>Re: Count no. of obs from multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606953#M176342</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I ran the code below and I got 'odsnumber' value as 19 and what I except is 20.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;

%let i=1;

data test&amp;amp;i.;
set sashelp.class(obs=1);
run;

%let i=2;

data test&amp;amp;i.;
set sashelp.class;
run;

proc sql noprint;
  select nobs
    into :odsnumber trimmed
    from sashelp.vtable
      where libname = 'WORK' and memname = "TEST&amp;amp;i."
  ;
quit;

%put &amp;amp;odsnumber.;

%mend;
%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I really suggest you stay away from using DATA steps to do this type of counting, as a data step has to read every single observation to count them all. This will be very time consuming. The approach from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;where you query the SASHELP.VTABLE, where the number of observations is stored, is the way to go.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 13:15:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/606953#M176342</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-25T13:15:03Z</dc:date>
    </item>
    <item>
      <title>Re: Count no. of obs from multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/607025#M176366</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I ran the code below and I got 'odsnumber' value as 19 and what I except is 20.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;

%let i=1;

data test&amp;amp;i.;
set sashelp.class(obs=1);
run;

%let i=2;

data test&amp;amp;i.;
set sashelp.class;
run;

proc sql noprint;
  select nobs
    into :odsnumber trimmed
    from sashelp.vtable
      where libname = 'WORK' and memname = "TEST&amp;amp;i."
  ;
quit;

%put &amp;amp;odsnumber.;

%mend;
%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You ONLY "counted" from "Test&amp;amp;i" when &amp;amp;I=2. So you only get the 19 from the second created data set.&lt;/P&gt;
&lt;P&gt;You might have been thinking about something similar to and memname like "%TEST%" or maybe substr(memname,1,4)="TEST" but the equals comparison only is going to look at a single value. That single value would have to be something that is not the data set complete name to find two data set values.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 16:02:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-no-of-obs-from-multiple-datasets/m-p/607025#M176366</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-25T16:02:32Z</dc:date>
    </item>
  </channel>
</rss>

