<?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: Conditionally printing two reports in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-printing-two-reports/m-p/66711#M14478</link>
    <description>Hi:&lt;BR /&gt;
  You should read about the difference between GLOBAL and LOCAL macro variables. If you create &amp;amp;ERROR in a macro program, generally &amp;amp;ERROR would be "local" in scope and therefore, would NOT be available AFTER %Error_chk had finished processing.&lt;BR /&gt;
 &lt;BR /&gt;
  Also, you do NOT show what's inside %history_rpt or %err_prt, however, unless they are data step programs, you do not need to invoke them INSIDE a DATA _NULL_ step (which seems to be happening in your %Report_selection macro program). A %IF test can be done inside a macro program without being in a datasetp program. In fact , if %history_rpt or %err_prt use ODS or SAS procedures, you're probably better off just getting rid of the DATA _NULL_  that you have in the second macro definition.&lt;BR /&gt;
&lt;BR /&gt;
Finally, I see where you set &amp;amp;ERROR to 1, but I don't see where you set or initialize it to 0. Macro variables are initially set to nothing or NULL, not zero. You might put  --&lt;BR /&gt;
[pre]&lt;BR /&gt;
%global error;&lt;BR /&gt;
%let error = 0;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
             &lt;BR /&gt;
before the proc sql step. &lt;BR /&gt;
&lt;BR /&gt;
That may not solve all your issues, however. You probably have a logic problem, as well. The first rule of writing a macro program is beginning with a working SAS program.  Your underlying assumption seems to be that you have an error if SQLOBS is 0. However, when I run the same query -- once to create a table and a second time to create a view, I get sqlobs=0 for the view. According to the documentation, SQLOBS always is 0 if a view is created (refer to to the documentation here:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/sqlproc/59727/HTML/default/a001360983.htm#a001404680)" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/sqlproc/59727/HTML/default/a001360983.htm#a001404680)&lt;/A&gt; And, you can prove it to yourself, by running these 2 SQL steps:&lt;BR /&gt;
[pre]&lt;BR /&gt;
1264  proc sql;&lt;BR /&gt;
1265    create table work.reg1 as&lt;BR /&gt;
1266    select * from sashelp.shoes&lt;BR /&gt;
1267    where region = 'Asia';&lt;BR /&gt;
NOTE: Table WORK.REG1 created, with 14 rows and 7 columns.&lt;BR /&gt;
                               &lt;BR /&gt;
1268    %put **** 1) create table sqlobs = &amp;amp;sqlobs;&lt;BR /&gt;
**** 1) create table sqlobs = 14&lt;BR /&gt;
1269  quit;&lt;BR /&gt;
NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;
      real time           0.01 seconds&lt;BR /&gt;
      cpu time            0.01 seconds&lt;BR /&gt;
                  &lt;BR /&gt;
             &lt;BR /&gt;
1270&lt;BR /&gt;
1271  proc sql;&lt;BR /&gt;
1272    create view WORK.reg2&lt;BR /&gt;
1273    as SELECT * FROM sashelp.shoes&lt;BR /&gt;
1274    where region = 'Asia';&lt;BR /&gt;
NOTE: SQL view WORK.REG2 has been defined.&lt;BR /&gt;
1275    %put **** 2) create view sqlobs = &amp;amp;sqlobs;&lt;BR /&gt;
**** 2) create view sqlobs = 0&lt;BR /&gt;
1276  Quit;&lt;BR /&gt;
NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;
      real time           0.01 seconds&lt;BR /&gt;
      cpu time            0.01 seconds&lt;BR /&gt;
                                         &lt;BR /&gt;
[/pre]&lt;BR /&gt;
                              &lt;BR /&gt;
    &lt;BR /&gt;
This means that even if &amp;amp;ERROR were available, it would always be set to 1, because as long as you are basing SQLOBS on view creation, it will always be 0.&lt;BR /&gt;
&lt;BR /&gt;
A different technique for testing whether there are any records in a dataset is to capture NOBS from dictionary.tables:&lt;BR /&gt;
[pre]&lt;BR /&gt;
  proc sql;&lt;BR /&gt;
    select (nobs - delobs) into :numobs&lt;BR /&gt;
    from dictionary.tables&lt;BR /&gt;
    where libname = "WORK" and memname = "REVISION_GT_1";&lt;BR /&gt;
  quit;&lt;BR /&gt;
%let numobs = &amp;amp;numobs;&lt;BR /&gt;
%put *** number of obs in dataset is: &amp;amp;numobs;&lt;BR /&gt;
            &lt;BR /&gt;
[/pre]&lt;BR /&gt;
           &lt;BR /&gt;
Then you could test &amp;amp;NUMOBS:&lt;BR /&gt;
%if &amp;amp;NUMOBS = 0 %then %do; %let error = 1; %end;&lt;BR /&gt;
&lt;BR /&gt;
OR, you could just use &amp;amp;NUMOBS instead of &amp;amp;ERROR in your macro program.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
    <pubDate>Fri, 19 Dec 2008 16:44:34 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2008-12-19T16:44:34Z</dc:date>
    <item>
      <title>Conditionally printing two reports</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-printing-two-reports/m-p/66710#M14477</link>
      <description>I am trying to proc print two different reports, based on whether there are any records in a dataset.  I have the reports in macros, so I thought it would be easy to call, but I'm very confused as to where and how to do it.&lt;BR /&gt;
&lt;BR /&gt;
Here's the code:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro Error_chk;&lt;BR /&gt;
/***************************************************************/&lt;BR /&gt;
/*       error check sql here for the case that the where      */&lt;BR /&gt;
/*         record_id selected returns no rows                  */&lt;BR /&gt;
/***************************************************************/&lt;BR /&gt;
proc sql;&lt;BR /&gt;
CREATE VIEW WORK.rec_select&lt;BR /&gt;
		AS SELECT   RECORD_ID    FROM WORK.revision_gt_1;&lt;BR /&gt;
  &lt;BR /&gt;
%if &amp;amp;sqlobs = 0 %then %do;&lt;BR /&gt;
   %let error = 1;&lt;BR /&gt;
%end;   &lt;BR /&gt;
Quit;&lt;BR /&gt;
%mend Error_chk;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro Report_selection;&lt;BR /&gt;
Data _null_;&lt;BR /&gt;
%if &amp;amp;error = 0 %then %do;&lt;BR /&gt;
 %history_rpt;&lt;BR /&gt;
 %end;&lt;BR /&gt;
%else %if &amp;amp;error = 1 %then %do;&lt;BR /&gt;
 %err_prt;&lt;BR /&gt;
 %end;&lt;BR /&gt;
 run;&lt;BR /&gt;
%mend Report_selection;&lt;BR /&gt;
&lt;BR /&gt;
%Error_chk; &lt;BR /&gt;
%Report_selection;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
********HERE'S THE ERROR MESSAGE I'M GETTING&lt;BR /&gt;
&lt;BR /&gt;
6         %macro Error_chk;&lt;BR /&gt;
17         /***************************************************************/&lt;BR /&gt;
18         /*       error check sql here for the case that the where      */&lt;BR /&gt;
19         /*         record_id selected returns no rows                  */&lt;BR /&gt;
20         /***************************************************************/&lt;BR /&gt;
21         proc sql;&lt;BR /&gt;
22         CREATE VIEW WORK.rec_select&lt;BR /&gt;
23         		AS SELECT   RECORD_ID    FROM WORK.revision_gt_1;&lt;BR /&gt;
24         &lt;BR /&gt;
25         %if &amp;amp;sqlobs = 0 %then %do;&lt;BR /&gt;
26            %let error = 1;&lt;BR /&gt;
27         %end;&lt;BR /&gt;
28         Quit;&lt;BR /&gt;
29         %mend Error_chk;&lt;BR /&gt;
30         &lt;BR /&gt;
31         &lt;BR /&gt;
32         %macro Report_selection;&lt;BR /&gt;
33         Data _null_;&lt;BR /&gt;
34         %if &amp;amp;error = 0 %then %do;&lt;BR /&gt;
35          %history_rpt;&lt;BR /&gt;
36          %end;&lt;BR /&gt;
37         %else %if &amp;amp;error = 1 %then %do;&lt;BR /&gt;
38          %err_prt;&lt;BR /&gt;
39          %end;&lt;BR /&gt;
40          run;&lt;BR /&gt;
41         %mend Report_selection;&lt;BR /&gt;
42         &lt;BR /&gt;
43         %Error_chk;&lt;BR /&gt;
NOTE: SQL view WORK.REC_SELECT has been defined.&lt;BR /&gt;
NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;
      real time           0.00 seconds&lt;BR /&gt;
      user cpu time       0.00 seconds&lt;BR /&gt;
      system cpu time     0.00 seconds&lt;BR /&gt;
      Memory                            27k&lt;BR /&gt;
      Page Faults                       0&lt;BR /&gt;
      Page Reclaims                     0&lt;BR /&gt;
      Page Swaps                        0&lt;BR /&gt;
      Voluntary Context Switches        3&lt;BR /&gt;
      Involuntary Context Switches      0&lt;BR /&gt;
      Block Input Operations            0&lt;BR /&gt;
2 The SAS System                                                                                     09:51 Friday, December 19, 2008&lt;BR /&gt;
&lt;BR /&gt;
      Block Output Operations           0&lt;BR /&gt;
      &lt;BR /&gt;
&lt;BR /&gt;
44         %Report_selection;&lt;BR /&gt;
WARNING: Apparent symbolic reference ERROR not resolved.&lt;BR /&gt;
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &lt;BR /&gt;
       &amp;amp;error = 0 &lt;BR /&gt;
ERROR: The macro REPORT_SELECTION will stop executing.&lt;BR /&gt;
45         &lt;BR /&gt;
46         %LET _CLIENTTASKLABEL=;&lt;BR /&gt;
47         %LET _EGTASKLABEL=;&lt;BR /&gt;
48         %LET _CLIENTPROJECTNAME=;&lt;BR /&gt;
49         %LET _SASPROGRAMFILE=;&lt;BR /&gt;
50         &lt;BR /&gt;
51         ;*';*";*/;quit;run;&lt;BR /&gt;
                     ____&lt;BR /&gt;
                     180&lt;BR /&gt;
&lt;BR /&gt;
ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;BR /&gt;
&lt;BR /&gt;
NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.00 seconds&lt;BR /&gt;
      user cpu time       0.00 seconds&lt;BR /&gt;
      system cpu time     0.00 seconds&lt;BR /&gt;
      Memory                            108k&lt;BR /&gt;
      Page Faults                       0&lt;BR /&gt;
      Page Reclaims                     0&lt;BR /&gt;
      Page Swaps                        0&lt;BR /&gt;
      Voluntary Context Switches        0&lt;BR /&gt;
      Involuntary Context Switches      0&lt;BR /&gt;
      Block Input Operations            0&lt;BR /&gt;
      Block Output Operations           0&lt;BR /&gt;
      &lt;BR /&gt;
&lt;BR /&gt;
52         ODS _ALL_ CLOSE;&lt;BR /&gt;
NOTE: ODS PDF(EGPDF) printed no output. &lt;BR /&gt;
      (This sometimes results from failing to place a RUN statement before the ODS PDF(EGPDF) CLOSE statement.)&lt;BR /&gt;
53         &lt;BR /&gt;
54         &lt;BR /&gt;
55         QUIT; RUN;&lt;BR /&gt;
56         &lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
THANKS,</description>
      <pubDate>Fri, 19 Dec 2008 15:56:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-printing-two-reports/m-p/66710#M14477</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-12-19T15:56:54Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally printing two reports</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-printing-two-reports/m-p/66711#M14478</link>
      <description>Hi:&lt;BR /&gt;
  You should read about the difference between GLOBAL and LOCAL macro variables. If you create &amp;amp;ERROR in a macro program, generally &amp;amp;ERROR would be "local" in scope and therefore, would NOT be available AFTER %Error_chk had finished processing.&lt;BR /&gt;
 &lt;BR /&gt;
  Also, you do NOT show what's inside %history_rpt or %err_prt, however, unless they are data step programs, you do not need to invoke them INSIDE a DATA _NULL_ step (which seems to be happening in your %Report_selection macro program). A %IF test can be done inside a macro program without being in a datasetp program. In fact , if %history_rpt or %err_prt use ODS or SAS procedures, you're probably better off just getting rid of the DATA _NULL_  that you have in the second macro definition.&lt;BR /&gt;
&lt;BR /&gt;
Finally, I see where you set &amp;amp;ERROR to 1, but I don't see where you set or initialize it to 0. Macro variables are initially set to nothing or NULL, not zero. You might put  --&lt;BR /&gt;
[pre]&lt;BR /&gt;
%global error;&lt;BR /&gt;
%let error = 0;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
             &lt;BR /&gt;
before the proc sql step. &lt;BR /&gt;
&lt;BR /&gt;
That may not solve all your issues, however. You probably have a logic problem, as well. The first rule of writing a macro program is beginning with a working SAS program.  Your underlying assumption seems to be that you have an error if SQLOBS is 0. However, when I run the same query -- once to create a table and a second time to create a view, I get sqlobs=0 for the view. According to the documentation, SQLOBS always is 0 if a view is created (refer to to the documentation here:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/sqlproc/59727/HTML/default/a001360983.htm#a001404680)" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/sqlproc/59727/HTML/default/a001360983.htm#a001404680)&lt;/A&gt; And, you can prove it to yourself, by running these 2 SQL steps:&lt;BR /&gt;
[pre]&lt;BR /&gt;
1264  proc sql;&lt;BR /&gt;
1265    create table work.reg1 as&lt;BR /&gt;
1266    select * from sashelp.shoes&lt;BR /&gt;
1267    where region = 'Asia';&lt;BR /&gt;
NOTE: Table WORK.REG1 created, with 14 rows and 7 columns.&lt;BR /&gt;
                               &lt;BR /&gt;
1268    %put **** 1) create table sqlobs = &amp;amp;sqlobs;&lt;BR /&gt;
**** 1) create table sqlobs = 14&lt;BR /&gt;
1269  quit;&lt;BR /&gt;
NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;
      real time           0.01 seconds&lt;BR /&gt;
      cpu time            0.01 seconds&lt;BR /&gt;
                  &lt;BR /&gt;
             &lt;BR /&gt;
1270&lt;BR /&gt;
1271  proc sql;&lt;BR /&gt;
1272    create view WORK.reg2&lt;BR /&gt;
1273    as SELECT * FROM sashelp.shoes&lt;BR /&gt;
1274    where region = 'Asia';&lt;BR /&gt;
NOTE: SQL view WORK.REG2 has been defined.&lt;BR /&gt;
1275    %put **** 2) create view sqlobs = &amp;amp;sqlobs;&lt;BR /&gt;
**** 2) create view sqlobs = 0&lt;BR /&gt;
1276  Quit;&lt;BR /&gt;
NOTE: PROCEDURE SQL used (Total process time):&lt;BR /&gt;
      real time           0.01 seconds&lt;BR /&gt;
      cpu time            0.01 seconds&lt;BR /&gt;
                                         &lt;BR /&gt;
[/pre]&lt;BR /&gt;
                              &lt;BR /&gt;
    &lt;BR /&gt;
This means that even if &amp;amp;ERROR were available, it would always be set to 1, because as long as you are basing SQLOBS on view creation, it will always be 0.&lt;BR /&gt;
&lt;BR /&gt;
A different technique for testing whether there are any records in a dataset is to capture NOBS from dictionary.tables:&lt;BR /&gt;
[pre]&lt;BR /&gt;
  proc sql;&lt;BR /&gt;
    select (nobs - delobs) into :numobs&lt;BR /&gt;
    from dictionary.tables&lt;BR /&gt;
    where libname = "WORK" and memname = "REVISION_GT_1";&lt;BR /&gt;
  quit;&lt;BR /&gt;
%let numobs = &amp;amp;numobs;&lt;BR /&gt;
%put *** number of obs in dataset is: &amp;amp;numobs;&lt;BR /&gt;
            &lt;BR /&gt;
[/pre]&lt;BR /&gt;
           &lt;BR /&gt;
Then you could test &amp;amp;NUMOBS:&lt;BR /&gt;
%if &amp;amp;NUMOBS = 0 %then %do; %let error = 1; %end;&lt;BR /&gt;
&lt;BR /&gt;
OR, you could just use &amp;amp;NUMOBS instead of &amp;amp;ERROR in your macro program.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Fri, 19 Dec 2008 16:44:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-printing-two-reports/m-p/66711#M14478</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-12-19T16:44:34Z</dc:date>
    </item>
  </channel>
</rss>

