<?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: Check sqlobs in each dataset and then create/drop dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-sqlobs-in-each-dataset-and-then-create-drop-dataset/m-p/861502#M340308</link>
    <description>&lt;P&gt;It is not clear what code you want to generate, but you can just save the SQLOBS results from the creation step into you own macro variables and then use that later if you want. In general it is good practice to save the SQLOBS result since its value will be overwritten by the results from the next SQL SELECT statement you run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example to just generate macro variables named COUNT1, COUNT2, etc. you could add this %LET statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let count&amp;amp;type_id = &amp;amp;sqlobs;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Feb 2023 17:27:13 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-02-28T17:27:13Z</dc:date>
    <item>
      <title>Check sqlobs in each dataset and then create/drop dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-sqlobs-in-each-dataset-and-then-create-drop-dataset/m-p/861499#M340307</link>
      <description>&lt;P&gt;I've the below macro code which create SAS dataset from database table and then I'm checking the record count and if it has record then it will drop and rename the SAS dataset. Instead of doing this record check and drop/rename the table for every iteration, I want to check the number of observations for each table and only if we there is record for all the tables then only drop/rename the table. Total number of SAS table creation is depends on the&amp;nbsp;number of values in the macro variable 'type_lt'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So after this line '&lt;EM&gt;&lt;STRONG&gt;%if &amp;amp;sqlobs %then %do;&lt;/STRONG&gt;&lt;/EM&gt;'&amp;nbsp; I would like&amp;nbsp;to know if we can create one more macro variable to add the counter when we have observation.of each&amp;nbsp; &lt;EM&gt;&lt;STRONG&gt;APP.t_&amp;amp;pmt_tbl_name and &lt;/STRONG&gt;&lt;/EM&gt;when we have observations all the tables then I have to execute drop/rename step. I'm not certain if the proc datasets step move outside of the loop.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assume we have four iterations for this macro then we have to drop/rename four datasets if we have obervation in&amp;nbsp;APP.t_&amp;amp;pmt_tbl_name all the iterations. Any help?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;% macro test;
proc sql;
%do type_id=1 %to %sysfunc(countw(&amp;amp;type_lt,,s));
........
%let sqlobs=0;	  
	 
create table APP.t_&amp;amp;pmt_tbl_name as
select * from connection to database(
select distinct &amp;amp;current_distinct_var_list
from &amp;amp;schema_temp..&amp;amp;source_tbl
        )
      ;
     
%if &amp;amp;sqlobs %then %do;
     
proc datasets lib=APP noprint;
delete &amp;amp;pmt_tbl_name;
change t_&amp;amp;pmt_tbl_name=&amp;amp;pmt_tbl_name;
quit;        
      ;
%end;
quit;
;
%end;
.........
%mend;

%let type_lt = 
  a*b*c
  d*e
  e*f*g*h
  a*g*i
;

%test(
  type_lt = &amp;amp;type_lt
)
&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Feb 2023 17:15:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-sqlobs-in-each-dataset-and-then-create-drop-dataset/m-p/861499#M340307</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2023-02-28T17:15:47Z</dc:date>
    </item>
    <item>
      <title>Re: Check sqlobs in each dataset and then create/drop dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-sqlobs-in-each-dataset-and-then-create-drop-dataset/m-p/861502#M340308</link>
      <description>&lt;P&gt;It is not clear what code you want to generate, but you can just save the SQLOBS results from the creation step into you own macro variables and then use that later if you want. In general it is good practice to save the SQLOBS result since its value will be overwritten by the results from the next SQL SELECT statement you run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example to just generate macro variables named COUNT1, COUNT2, etc. you could add this %LET statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let count&amp;amp;type_id = &amp;amp;sqlobs;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 17:27:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-sqlobs-in-each-dataset-and-then-create-drop-dataset/m-p/861502#M340308</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-28T17:27:13Z</dc:date>
    </item>
    <item>
      <title>Re: Check sqlobs in each dataset and then create/drop dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-sqlobs-in-each-dataset-and-then-create-drop-dataset/m-p/861508#M340311</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; In the below step we are checking the value of sqlobs and then doing the delete/rename for each iteration.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%if &amp;amp;sqlobs %then %do;
     
proc datasets lib=APP noprint;
delete &amp;amp;pmt_tbl_name;
change t_&amp;amp;pmt_tbl_name=&amp;amp;pmt_tbl_name;
quit;        
      ;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I want to break that approach into parts.&amp;nbsp; a) Check the value of sqlobs of each iteration. If it's greater than 0 then create one macro varible like % let &lt;EM&gt;&lt;STRONG&gt;counter&lt;/STRONG&gt;&lt;/EM&gt;=1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there are three iterations in the macro and if we have observations in the each of the dataset then value of &lt;EM&gt;&lt;STRONG&gt;counter&lt;/STRONG&gt;&lt;/EM&gt; macro&amp;nbsp;varible should increase to 3. Now number of iterations is same as value of &lt;EM&gt;&lt;STRONG&gt;counter&lt;/STRONG&gt;&lt;/EM&gt; macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;b) if the of&amp;nbsp;number of iterations is equal to value of &lt;EM&gt;&lt;STRONG&gt;counter&lt;/STRONG&gt;&lt;/EM&gt; macro variable then execute the below step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc datasets lib=APP noprint;
delete &amp;amp;pmt_tbl_name;
change t_&amp;amp;pmt_tbl_name=&amp;amp;pmt_tbl_name;
quit;  &lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Feb 2023 17:45:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-sqlobs-in-each-dataset-and-then-create-drop-dataset/m-p/861508#M340311</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2023-02-28T17:45:04Z</dc:date>
    </item>
    <item>
      <title>Re: Check sqlobs in each dataset and then create/drop dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-sqlobs-in-each-dataset-and-then-create-drop-dataset/m-p/861517#M340316</link>
      <description>&lt;P&gt;You can use %EVAL() to do simple integer arithmetic. SAS will evaluate boolean expression to 1 for TRUE and 0 for FALSE.&amp;nbsp; So to increment when SQLOBS is not zero you could use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let counter=%eval(&amp;amp;counter + (&amp;amp;sqlobs&amp;gt;0));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if your logic gets more complex you will probably want to abandon pure macro code and instead put the data into a dataset where you can use the full power of the actual SAS language instead of the minimal computational power of the macro language.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data count;
  stop;
  length dsname $100 count 8;
run;
proc sql;
....
insert into count(dsname,count) values ("&amp;amp;schema..&amp;amp;table",&amp;amp;sqlobs);
...&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Feb 2023 17:56:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-sqlobs-in-each-dataset-and-then-create-drop-dataset/m-p/861517#M340316</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-28T17:56:41Z</dc:date>
    </item>
    <item>
      <title>Re: Check sqlobs in each dataset and then create/drop dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-sqlobs-in-each-dataset-and-then-create-drop-dataset/m-p/861532#M340322</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;I think I'm missing something from your idea. I tried the following code by adding counter macro variable and it is errored. I don't see any delete and rename dataset step in your code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assume if we have three iterations and the value of 'counter' macro variable is also then i want to delete the already existing tables and rename the tables. This step also should execute three times after the value of macro variable is resolved to 3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%if &amp;amp;nobs %then %do;
%let counter=%eval(&amp;amp;counter + (&amp;amp;sqlobs&amp;gt;0));
%put ####Counter=&amp;amp;counter.;
      
      create table &amp;amp;prompt_table_name as
        select * from connection to database(
          select *
          from &amp;amp;schema_temp..&amp;amp;prompt_tbl_name;
        )
      ;
%end;&lt;/PRE&gt;
&lt;P&gt;Error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;SYMBOLGEN:  Macro variable NOBS resolves to 887
MLOGIC(TEST):  %IF condition &amp;amp;nobs is TRUE
MLOGIC(TEST):  %LET (variable name is COUNTER)
WARNING: Apparent symbolic reference COUNTER not resolved.
SYMBOLGEN:  Macro variable SQLOBS resolves to 1
WARNING: Apparent symbolic reference COUNTER not resolved.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       &amp;amp;counter + (1&amp;gt;0) 
ERROR: The macro TEST will stop executing.
MLOGIC(TEST):  Ending execution.&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Feb 2023 18:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-sqlobs-in-each-dataset-and-then-create-drop-dataset/m-p/861532#M340322</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2023-02-28T18:29:38Z</dc:date>
    </item>
  </channel>
</rss>

