<?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: How to summarize a bunch of queries in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642470#M191666</link>
    <description>&lt;P&gt;Is the WHERE condition always based on the first variable in the sort nodupkey?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, your code can be optimized:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let question=Q1;
%let varlist=var1 var2 var3;
%let value=xy;

proc sort
  data=mydatabase (
    keep=&amp;amp;varlist
    where=(%scan(&amp;amp;varlist,1) = "&amp;amp;value")
  )
  out=sorted
  nodupkey
;
by &amp;amp;varlist;
run;

data single_result;
question = "&amp;amp;question";
count = nobs;
set sorted nobs=nobs;
stop;
keep question count;
run;

proc&amp;nbsp;append&amp;nbsp;data=single_result&amp;nbsp;base=want&amp;nbsp;force;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If my assumptions were correct, this can form the basis of a macro with three parameters. Create a dataset with those three parameters, so you can run the macro in an automated way. Before running that, initialize the want dataset.&lt;/P&gt;</description>
    <pubDate>Fri, 24 Apr 2020 08:07:53 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-04-24T08:07:53Z</dc:date>
    <item>
      <title>How to summarize a bunch of queries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642463#M191664</link>
      <description>&lt;P&gt;Hi everybody&lt;/P&gt;&lt;P&gt;In order to fill out a questionnaire I need to do some (around 300) queries in our database and extract the number of cases where certain conditions are true. My SAS-Code is created dynamically by a PHP-Script and looks like this (it's actually a loop over all queries):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/******************************************
Question "Q1"
******************************************/
DATA want (KEEP = var1 var2 var3);
SET myDatabase;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;PROC SORT NODUPKEY;&lt;BR /&gt;BY var1, var2, var3;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;   SELECT "Q1" AS question, COUNT(*) AS count FROM want WHERE var1='xy';&lt;BR /&gt;QUIT;&lt;BR /&gt;&lt;BR /&gt;/****************************************** &lt;BR /&gt;Question "Q2" &lt;BR /&gt;******************************************/ &lt;BR /&gt;DATA want (KEEP = var4 var5 var6); &lt;BR /&gt;SET myDatabase;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;PROC SORT NODUPKEY;&lt;BR /&gt;BY var4, var5, var6;&lt;BR /&gt;RUN;&lt;BR /&gt;&lt;BR /&gt;PROC SQL;&lt;BR /&gt;SELECT "Q2" AS question, COUNT(*) AS count FROM want WHERE var4='y';&lt;BR /&gt;QUIT;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;... and so forth&lt;/PRE&gt;&lt;P&gt;Now, I need to summarise these results somehow, so that I get a table like this:&lt;/P&gt;&lt;PRE&gt;Question | Result
---------------------- &lt;BR /&gt;Q1         120&lt;BR /&gt;Q2         240&lt;BR /&gt;Q3         1&lt;/PRE&gt;&lt;P&gt;My idea is that at the beginning I create an empty table `results` with two columns (name of the query and result). Then in each code-block (Q1, Q2, ... Qx) I append one line to the results-table (name of the query and the result itself).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But how can I do this? How can I save the result of each PROC SQL in an existing table?&lt;/P&gt;&lt;P&gt;Do you see any better solution?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 07:20:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642463#M191664</guid>
      <dc:creator>dstuder</dc:creator>
      <dc:date>2020-04-24T07:20:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to summarize a bunch of queries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642469#M191665</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324367"&gt;@dstuder&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suggest that you adapt the code as below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/******************************************
Question "Q1"
******************************************/
DATA want (KEEP = var1 var2 var3);
SET myDatabase;
RUN;

PROC SORT NODUPKEY;
BY var1 var2 var3; 	/* &amp;lt;---- REMOVE COMMAS */
RUN;

PROC SQL;
   CREATE TABLE Q1 AS /* &amp;lt;---- ADD THIS */
   SELECT "Q1" AS question, COUNT(*) AS count FROM want WHERE var1='xy';
QUIT;

/****************************************** 
Question "Q2" 
******************************************/ 
DATA want (KEEP = var4 var5 var6); 
SET myDatabase;
RUN;

PROC SORT NODUPKEY;
BY var4 var5 var6; /* &amp;lt;---- REMOVE COMMAS */
RUN;

PROC SQL;
	CREATE TABLE Q2 AS /* &amp;lt;---- ADD THIS */
	SELECT "Q2" AS question, COUNT(*) AS count FROM want WHERE var4='y';
QUIT;

/**
	....
**/

/* APPEND */

DATA FINAL;
	LENGTH question $ 100;
	SET Q:;
	RENAME count = result;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 07:56:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642469#M191665</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-04-24T07:56:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to summarize a bunch of queries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642470#M191666</link>
      <description>&lt;P&gt;Is the WHERE condition always based on the first variable in the sort nodupkey?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, your code can be optimized:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let question=Q1;
%let varlist=var1 var2 var3;
%let value=xy;

proc sort
  data=mydatabase (
    keep=&amp;amp;varlist
    where=(%scan(&amp;amp;varlist,1) = "&amp;amp;value")
  )
  out=sorted
  nodupkey
;
by &amp;amp;varlist;
run;

data single_result;
question = "&amp;amp;question";
count = nobs;
set sorted nobs=nobs;
stop;
keep question count;
run;

proc&amp;nbsp;append&amp;nbsp;data=single_result&amp;nbsp;base=want&amp;nbsp;force;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If my assumptions were correct, this can form the basis of a macro with three parameters. Create a dataset with those three parameters, so you can run the macro in an automated way. Before running that, initialize the want dataset.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 08:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642470#M191666</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-24T08:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to summarize a bunch of queries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642471#M191667</link>
      <description>&lt;P&gt;Hello Kurt,&lt;BR /&gt;actually no. The where-condition is random and depends entirely on what the specific question in the questionnaire asks for. It can contain one or several variables.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 08:26:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642471#M191667</guid>
      <dc:creator>dstuder</dc:creator>
      <dc:date>2020-04-24T08:26:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to summarize a bunch of queries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642472#M191668</link>
      <description>&lt;P&gt;Sorry for the bad code. It was more of a pseudo-code in order to show the problem and I didn't actually run it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DATA FINAL;
	LENGTH question $ 100;
	SET Q:;
	RENAME count = result;
RUN;&lt;/PRE&gt;&lt;P&gt;So does this code actually summarize all the tables Q1, Q2, ... Q300 (created by PROC SQL) and put the table names and number of cases in a table `final`? I am also asking myself whether it wouldn't be better to overwrite a single table "Q" for each question instead of creating hundreds of tables called Q1, Q2 etc.?&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 08:23:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642472#M191668</guid>
      <dc:creator>dstuder</dc:creator>
      <dc:date>2020-04-24T08:23:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to summarize a bunch of queries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642474#M191670</link>
      <description>&lt;P&gt;"Any kind of code" parameters in macros are quite tricky to handle. In this case I suggest you use call execute (or dosubl) from within a data step to create the code.&lt;/P&gt;
&lt;P&gt;But the basic structure of my code is still valid: use the where condition and keep= in the first step that reads data and does the sort, and then get the number of observations.&lt;/P&gt;
&lt;P&gt;Note that my code overwrites the temporary dataset single_result in every iteration and appends it to the final table, so you'll only have two datasets in the end, one of which can safely be discarded.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 08:30:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642474#M191670</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-24T08:30:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to summarize a bunch of queries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642475#M191671</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324367"&gt;@dstuder&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The CREATE TABLE xx AS statement just before the SELECT clause in PROC SQL put the results of the query in a dataset xx.&lt;/P&gt;
&lt;P&gt;-&amp;gt; So in the code provided, it will create datasets Q1, Q2, ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the FINAL dataset, SAS append all tables beginning with the letter Q (-&amp;gt; Q&lt;FONT color="#FF00FF"&gt;:&lt;/FONT&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Definitely, you can avoid creating hundreds of intermediate datasets.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Here is a way to proceed:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/******************************************
Question "Q1"
******************************************/
DATA want1 (KEEP = var1 var2 var3); /* &amp;lt;--- RENAME WANT*/
SET myDatabase;
RUN;

PROC SORT NODUPKEY;
BY var1 var2 var3; 	/* &amp;lt;---- REMOVE COMMAS */
RUN;

/****************************************** 
Question "Q2" 
******************************************/ 
DATA want2 (KEEP = var4 var5 var6);  /* &amp;lt;--- RENAME WANT*/
SET myDatabase;
RUN;

PROC SORT NODUPKEY;
BY var4 var5 var6; /* &amp;lt;---- REMOVE COMMAS */
RUN;


/* APPEND */

PROC SQL;
   CREATE TABLE FINAL AS /* &amp;lt;---- ADD THIS */
   SELECT "Q1" AS question, COUNT(*) AS count FROM want1 WHERE var1='xy' /* ADAPT WANT1*/
	UNION /* &amp;lt;---- ADD THIS */
   SELECT "Q2" AS question, COUNT(*) AS count FROM want2 WHERE var4='y' /* ADAPT WANT2*/
    UNION
   /*...*/
	;
QUIT;&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 08:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642475#M191671</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-04-24T08:31:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to summarize a bunch of queries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642476#M191672</link>
      <description>&lt;P&gt;Thanks! I'll have to go through your answer carfully as I am still a newbie in SAS.&lt;BR /&gt;&lt;BR /&gt;A short follow-up question: Does it make any difference in terms of performance whether I use a macro or if I just repeat all the code for each question? I mean if it's just a question of readability I dont really care because I am creating the SAS-code automatically using a PHP-script.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 08:38:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642476#M191672</guid>
      <dc:creator>dstuder</dc:creator>
      <dc:date>2020-04-24T08:38:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to summarize a bunch of queries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642477#M191673</link>
      <description>&lt;P&gt;The following SQL construct can be used to insert (i.e. append) a query result into an existing table&lt;/P&gt;
&lt;PRE&gt;INSERT INTO &amp;lt;TABLE&amp;gt;
SELECT ... ;&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;* blank base table for results;&lt;BR /&gt;proc sql;
  create table work.results 
  (Question CHAR(20), Result NUM);


* Dynamic 1 .... ;

data dynamic1; set sashelp.class; run;
&lt;BR /&gt;* Append query results for Q1 to results table;
proc sql;
  insert into results
  select 'Q1', count(*) from dynamic1;

* Dynamic 2 .... ;

data dynamic2; set sashelp.class; where age &amp;lt; 14; run;

* Append query results for Q2 to results table; &lt;BR /&gt;proc sql;
  insert into results
  select 'Q2', count(*) from dynamic2;

%let syslast = results;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Apr 2020 08:40:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642477#M191673</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-04-24T08:40:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to summarize a bunch of queries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642481#M191675</link>
      <description>&lt;P&gt;Creating the code from a php script will probably mean you write a program, run it in batch, rewrite it, run it again, and so on. Why not use the tools that SAS provides for creating code dynamically?&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 08:55:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642481#M191675</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-24T08:55:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to summarize a bunch of queries</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642602#M191723</link>
      <description>&lt;P&gt;This looks a lot like skip pattern verification. If your current data is one record per respondent I would use a data step instead of hundreds of queries in SQL.&lt;/P&gt;
&lt;P&gt;My basic approach when I have done this is to create a bunch of indicator variables 1 (for correct/desired/expected value of the variable with conditions) and 0 else. Then use a summary or report step to sum the 1 values to get one output table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;More dummy /pseudo code:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   Q1 = (var1 ='xy');
   Q2 = (var4 ='y');
   /* skip pattern check: expect some value of Var5 
      when var3='y'
   */
   Q3 = (not missing(var5) and var3='y');

run;

proc summary data=want ;
   var Q1 - Q3;
   output out=work.summary sum=;   /* a mean would be percent of records
                                      with the response*/
run;

/* or a report procedure*/
proc tabulate data=want;
   var Q1 -Q3;
   table Q1 -Q3,
         sum
   ;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Apr 2020 14:13:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-summarize-a-bunch-of-queries/m-p/642602#M191723</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-24T14:13:40Z</dc:date>
    </item>
  </channel>
</rss>

