<?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: Using Fetch function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-Fetch-function/m-p/89807#M19065</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;BR /&gt;Like Tom suggested, in general you can't do it in one step. However, it can be cheated using Hash():&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12pt; font-family: Calibri;"&gt;&lt;SPAN lang="EN"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;data _null_;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;if _n_=1 then do;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;if 0 then set mstate;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;declare hash h1(dataset:’mstate (where=(state=’NC’))’, multidata:’y’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h1.definekey(‘state’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h1.definedata(all:’y’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h1.definedone();&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;declare hash h2(dataset:’mstate (where=(not missing(rep)))’, multidata:’y’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h2.definekey(‘rep’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h2.definedata(all:’y’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h2.definedone();&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;declare hash h3(dataset:’mstate’, multidata:’y’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h3.definekey(‘rep’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h3.definedata(all:’y’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h3.definedone();&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;rc=h1.output(dataset:’lstate’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;do until (last);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;set tot end=last;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;if h2.find()=0 then h3.add();&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;if last then h3.output(dateset:’mstate’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;stop;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Haikuo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 04 Dec 2012 19:17:11 GMT</pubDate>
    <dc:creator>Haikuo</dc:creator>
    <dc:date>2012-12-04T19:17:11Z</dc:date>
    <item>
      <title>Using Fetch function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Fetch-function/m-p/89805#M19063</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can any one give me some examples of using fetch function in base sas .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example&lt;/P&gt;&lt;P&gt;In one step i need to create a table with required values and at the same time i need to INSERT VALUES into another table &lt;/P&gt;&lt;P&gt;For example i have a data set STATE&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA LSTATE MSTATE;&lt;/P&gt;&lt;P&gt;Instead of SET i need to use Fetch function and pass each observation and check the values.&lt;/P&gt;&lt;P&gt;In this step i need to output Lstate when the id ='NC' and i need to check Rep if Rep ne&amp;nbsp; ' ' then i need to do&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;Insert into mstate&lt;/P&gt;&lt;P&gt;select * from Tot where Rep=&amp;amp;Rep from STATE;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 Dec 2012 11:10:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Fetch-function/m-p/89805#M19063</guid>
      <dc:creator>JasonNC</dc:creator>
      <dc:date>2012-12-04T11:10:59Z</dc:date>
    </item>
    <item>
      <title>Re: Using Fetch function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Fetch-function/m-p/89806#M19064</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I doubt you can do that in a single step. &lt;/P&gt;&lt;P&gt;I am not clear about what you want to do, but it looks like you want to generate LSTATE from STATE and also generate a list of REP values to use to pull information from TOT to insert into MSTATE. You should be able to generate LSTATE and the list of REP values in one step. Then you can use the list of REP values to insert records from TOT into MSTATE.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data lstate replist(keep=rep);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set state;&lt;/P&gt;&lt;P&gt; if state='NC' then output lstate ;&lt;/P&gt;&lt;P&gt; if rep ne ' ' then output replist;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;proc sql ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; insert into mstate select * from tot where rep in (select rep from replist);&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 Dec 2012 12:51:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Fetch-function/m-p/89806#M19064</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-12-04T12:51:40Z</dc:date>
    </item>
    <item>
      <title>Re: Using Fetch function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Fetch-function/m-p/89807#M19065</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;BR /&gt;Like Tom suggested, in general you can't do it in one step. However, it can be cheated using Hash():&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12pt; font-family: Calibri;"&gt;&lt;SPAN lang="EN"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;data _null_;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;if _n_=1 then do;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;if 0 then set mstate;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;declare hash h1(dataset:’mstate (where=(state=’NC’))’, multidata:’y’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h1.definekey(‘state’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h1.definedata(all:’y’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h1.definedone();&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;declare hash h2(dataset:’mstate (where=(not missing(rep)))’, multidata:’y’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h2.definekey(‘rep’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h2.definedata(all:’y’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h2.definedone();&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;declare hash h3(dataset:’mstate’, multidata:’y’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h3.definekey(‘rep’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h3.definedata(all:’y’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;h3.definedone();&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;rc=h1.output(dataset:’lstate’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;do until (last);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;set tot end=last;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;if h2.find()=0 then h3.add();&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;if last then h3.output(dateset:’mstate’);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;stop;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P align="left" dir="ltr"&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Haikuo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 04 Dec 2012 19:17:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Fetch-function/m-p/89807#M19065</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2012-12-04T19:17:11Z</dc:date>
    </item>
    <item>
      <title>Re: Using Fetch function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Fetch-function/m-p/89808#M19066</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The problem I&amp;nbsp; am having is&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my case i have a data set with some thing about 10 million and i need to do a look up on DB2 table which contains about 600 million records based on iD and pull some information for that ID.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When i am doing subquery like this&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Proc sql noprint;&lt;/P&gt;&lt;P&gt;select id,info&lt;/P&gt;&lt;P&gt;from db2.table&lt;/P&gt;&lt;P&gt;where id in (select id from sas data set)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The problem here is when this query goes to Db2 the query against d2 table is going the query other than subquery is sent to db2&amp;nbsp; and pulling the entire 600 million records and then executing the subquery as a result it is showing DB2 CLI cursor error.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please let me know how to achieve this efficiently.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Dec 2012 11:22:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Fetch-function/m-p/89808#M19066</guid>
      <dc:creator>JasonNC</dc:creator>
      <dc:date>2012-12-05T11:22:16Z</dc:date>
    </item>
    <item>
      <title>Re: Using Fetch function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Fetch-function/m-p/89809#M19067</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You could see if you can get this libname option to work.&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #1f497d;"&gt;MULTI_DATASRC_OPT=in_clause&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #1f497d;"&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/acreldb/65247/HTML/default/viewer.htm#n0tcetvx1zpnayn1r8actrkjrd5o.htm"&gt;http://support.sas.com/documentation/cdl/en/acreldb/65247/HTML/default/viewer.htm#n0tcetvx1zpnayn1r8actrkjrd5o.htm&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #1f497d;"&gt;You might need to reformat the query as a join.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #1f497d;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #1f497d;"&gt;If the number of values is small enough then you could put them into a macro variable using SQL query. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #1f497d;"&gt;For character variables:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;select quote(trim(rep)) into :replist separated by ',' from replist ;&lt;/P&gt;&lt;P&gt;For numeric variable&lt;/P&gt;&lt;P&gt;select rep into :replist separated by ',' from replist;&lt;/P&gt;&lt;P&gt;Then you can use the list with an IN operator.&lt;/P&gt;&lt;P&gt;select * from db2.tot where rep in (&amp;amp;replist) ;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Dec 2012 12:31:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Fetch-function/m-p/89809#M19067</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-12-05T12:31:07Z</dc:date>
    </item>
  </channel>
</rss>

