<?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: Trying to run a query off another query in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584358#M166425</link>
    <description>&lt;P&gt;Thanks Paige&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It was my intent not to bog everyone down with unnecessary minutiae just the bare facts. Also, memno is in the data set First, Begdate is not I just used Date, my bad. The input is Hippa confidential and i did not want to include it. The point is, I got a return of over 750 distinct begdates and of course I could just use them in my macro VARIABLE (I am not all that savvy with the language, thats what I am here for, for answers from kind and knowledgeable people, not those who get a kick out of berating those not as knowledgeable as themselves).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The bottom line is, I wanted to use the list of COMBO's to run against the database to retrieve data without having to insert all 750 distinct combo's. I hope that clarifies for anyone that able to assist.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Tue, 27 Aug 2019 20:16:59 GMT</pubDate>
    <dc:creator>wheddingsjr</dc:creator>
    <dc:date>2019-08-27T20:16:59Z</dc:date>
    <item>
      <title>Trying to run a query off another query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584338#M166409</link>
      <description>&lt;P&gt;Hi all&lt;/P&gt;&lt;P&gt;I am trying to run a query off another query. Here is an example without attaching the VERY long query. I ran a query using this criteria:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#99cc00"&gt;/** MACROS **/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;%LET&lt;/FONT&gt; RANGEBEG = '2019-01-01';&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;%LET&lt;/FONT&gt; RANGEEND = '2019-07-31';&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;%LET&lt;/FONT&gt; PAIDRANGE = '2019-07-31';&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;%LET&lt;/FONT&gt; CODE = svc.BILL_SVC_UNIT_1_CD IN ('92002','92004','92012','92014');&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;%LET&lt;/FONT&gt; CODE2 = svc.BILL_SVC_UNIT_2_CD IN ('0920');&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;%LET&lt;/FONT&gt; PAID = svc.CL_SVC_INTL_MSG_1_DSC LIKE 'PAY%';&lt;/P&gt;&lt;P&gt;&lt;FONT color="#99cc00"&gt;/**END MACROS **/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt; &lt;STRONG&gt;SQL&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;CONNECT&lt;/FONT&gt; to teradata as td ( tdpid='pg' mode=teradata);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;create table&lt;/FONT&gt; First &lt;FONT color="#0000ff"&gt;AS&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;select&lt;/FONT&gt; * &lt;FONT color="#0000ff"&gt;from&lt;/FONT&gt; connection to td&lt;/P&gt;&lt;P&gt;&amp;nbsp; (&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;SELECT&lt;/FONT&gt; MEMNO&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,DATE&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,DATA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,DATA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,DATA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;FROM&lt;/FONT&gt; (My Database)&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;WHERE&lt;/FONT&gt; (MY INFO)&lt;/P&gt;&lt;P&gt;);&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; Final;&lt;/P&gt;&lt;P&gt;set First;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#99cc00"&gt;**** THIS SET CREATES THE COMBO COLUMN ****;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;COMBO = trim(left(MEMNO))||trim(left(BEGDATE));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt; &lt;STRONG&gt;SQL&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366ff"&gt;CREATE&lt;/FONT&gt; Table Results as&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366ff"&gt;SELECT&lt;/FONT&gt; Distinct*&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366ff"&gt;FROM&lt;/FONT&gt; First&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;QUIT&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I got the data that I wanted, now what I wanted to do is get a list of all the distinct COMBO's in the data so I ran this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt; &lt;STRONG&gt;SQL&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366ff"&gt;CREATE&lt;/FONT&gt; &lt;FONT color="#3366ff"&gt;Table&lt;/FONT&gt; Results2 &lt;FONT color="#3366ff"&gt;as&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366ff"&gt;Select Distinct&lt;/FONT&gt; COMBO&lt;/P&gt;&lt;P&gt;&lt;FONT color="#3366ff"&gt;From&lt;/FONT&gt; RESULTS;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;QUIT&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This gave me the list of distinct COMBO’s. Now I want to go back into the database and pull all other lines that uses that same COMBO that DOES NOT include whats in the macros from above, but the column COMBO doesn’t exist in the database. I hope this makes sense and any help on this would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2019 19:41:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584338#M166409</guid>
      <dc:creator>wheddingsjr</dc:creator>
      <dc:date>2019-08-27T19:41:39Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to run a query off another query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584353#M166420</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Final;
set First;
**** THIS SET CREATES THE COMBO COLUMN ****;
COMBO = trim(left(MEMNO))||trim(left(BEGDATE));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There is no BEGDATE in data set FIRST. Nevertheless, you could create this COMBO variable in your PROC SQL something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select memno,
begdate,
cats(memno,begdate) as combo&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Moving on&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;This gave me the list of distinct COMBO’s. Now I want to go back into the database and pull all other lines that uses that same COMBO that DOES NOT include whats in the macros from above, but the column COMBO doesn’t exist in the database. I hope this makes sense and any help on this would be greatly appreciated.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;You could still do joins/queries in the database on&amp;nbsp;&lt;CODE class=" language-sas"&gt;cats(memno,begdate)&lt;/CODE&gt; even if combo is not in the database.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;it would sure be nice if you gave us some input and output so we could see what you want to do. The phrase "DOES NOT include whats in the macros from above" is hard to decipher, as nowhere have you used the macro variables in any of your code and its not obvious how you want to use the macro variables.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Also, for clarity, you do not have macros, you have macro variables.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2019 20:09:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584353#M166420</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-27T20:09:31Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to run a query off another query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584358#M166425</link>
      <description>&lt;P&gt;Thanks Paige&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It was my intent not to bog everyone down with unnecessary minutiae just the bare facts. Also, memno is in the data set First, Begdate is not I just used Date, my bad. The input is Hippa confidential and i did not want to include it. The point is, I got a return of over 750 distinct begdates and of course I could just use them in my macro VARIABLE (I am not all that savvy with the language, thats what I am here for, for answers from kind and knowledgeable people, not those who get a kick out of berating those not as knowledgeable as themselves).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The bottom line is, I wanted to use the list of COMBO's to run against the database to retrieve data without having to insert all 750 distinct combo's. I hope that clarifies for anyone that able to assist.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2019 20:16:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584358#M166425</guid>
      <dc:creator>wheddingsjr</dc:creator>
      <dc:date>2019-08-27T20:16:59Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to run a query off another query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584359#M166426</link>
      <description>&lt;P&gt;Pseudo-code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
     create table something as select somevariables from database
          where cats(memno,begdate) in (select distinct combo from results);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Aug 2019 20:21:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584359#M166426</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-27T20:21:57Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to run a query off another query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584375#M166443</link>
      <description>&lt;P&gt;Thanks again Paige. What I did instead was just ran the&amp;nbsp;query and as previously stated ran the code to create the 750 distinct COMBO table. I then ran the entire query again, this time removing the below macro &lt;STRONG&gt;variables&lt;/STRONG&gt; (unfortunately for me that garnered 7.8 million lines).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%LET CODE = svc.BILL_SVC_UNIT_1_CD IN ('92002','92004','92012','92014');&lt;/P&gt;&lt;P&gt;%LET CODE2 = svc.BILL_SVC_UNIT_2_CD IN ('0920');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I then just ran the following code to get my final results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt; &lt;STRONG&gt;SQL&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;create table New as&lt;/P&gt;&lt;P&gt;SELECT *&lt;/P&gt;&lt;P&gt;FROM 2nd Query run&lt;/P&gt;&lt;P&gt;WHERE COMBO IN (select COMBO from DISTINCT COMBO TABLE);&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;QUIT&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I’m sure there was an easier way to do that, but I got my results. My main original question was how do I use the table I created that showed all 750 distinct combo’s and how to use that list to get all the other like combos from the database. Apparently, it was my lack of SAS terminology etc. that made you not quite understand what I was asking. Thanks nonetheless, your suggestions did point me in a direction where I was able to get the information I needed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2019 21:23:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584375#M166443</guid>
      <dc:creator>wheddingsjr</dc:creator>
      <dc:date>2019-08-27T21:23:14Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to run a query off another query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584384#M166449</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114971"&gt;@wheddingsjr&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks again Paige. What I did instead was just ran the&amp;nbsp;query and as previously stated ran the code to create the 750 distinct COMBO table. I then ran the entire query again, this time removing the below macro &lt;STRONG&gt;variables&lt;/STRONG&gt; (unfortunately for me that garnered 7.8 million lines).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%LET CODE = svc.BILL_SVC_UNIT_1_CD IN ('92002','92004','92012','92014');&lt;/P&gt;
&lt;P&gt;%LET CODE2 = svc.BILL_SVC_UNIT_2_CD IN ('0920');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I then just ran the following code to get my final results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;PROC&lt;/STRONG&gt; &lt;STRONG&gt;SQL&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;create table New as&lt;/P&gt;
&lt;P&gt;SELECT *&lt;/P&gt;
&lt;P&gt;FROM 2nd Query run&lt;/P&gt;
&lt;P&gt;WHERE COMBO IN (select COMBO from DISTINCT COMBO TABLE);&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;QUIT&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I’m sure there was an easier way to do that, but I got my results. My main original question was how do I use the table I created that showed all 750 distinct combo’s and how to use that list to get all the other like combos from the database. Apparently, it was my lack of SAS terminology etc. that made you not quite understand what I was asking. Thanks nonetheless, your suggestions did point me in a direction where I was able to get the information I needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That sounds more like a JOIN might be appropriate:&lt;/P&gt;
&lt;P&gt;Very generic code:&lt;/P&gt;
&lt;PRE&gt;proc sql; 
   create table combined as 
   select &amp;lt;list the b.variables here&amp;gt;
   from uniquedata as a
        left join
        someotherdata as b
        on a.keyvar1=b.keyvar1
        and a.keyvar2=b.keyvar2
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;which matches all of the key values (assumes 2 variables with same names in the sets but the names need not be the same, just the values) in the unique data set with the matches. The A and B are aliases to differentiate between which data set a variable comes from.&lt;/P&gt;
&lt;P&gt;In this example if you have a unique combo of a.keyvar1= ABC and a.keyvar2 = 456 then ALL of the records in&amp;nbsp; the B data set with those values are returned. List the variables you want such as b.var1, b.var3, b.varx from the b set. If there are values in the A set that you want that aren't in B then add them&amp;nbsp;: Select &amp;nbsp; b.var1, b.var3, b.varx&amp;nbsp;, a.somevar, a.othervar&lt;/P&gt;
&lt;P&gt;Left join would have a record for each combination of the A set keyvars including those without matches, other types of joins such as Intersect , would have different results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2019 23:26:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-run-a-query-off-another-query/m-p/584384#M166449</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-08-27T23:26:20Z</dc:date>
    </item>
  </channel>
</rss>

