<?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: retaining values in a %do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42963#M8786</link>
    <description>How about &lt;BR /&gt;
if substr(company_identifer , 1, 6) in ( &amp;amp;id ) then output;&lt;BR /&gt;
&lt;BR /&gt;
just create your list for ID quoted with a comma delimiter.</description>
    <pubDate>Tue, 19 Jan 2010 18:40:50 GMT</pubDate>
    <dc:creator>Flip</dc:creator>
    <dc:date>2010-01-19T18:40:50Z</dc:date>
    <item>
      <title>retaining values in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42962#M8785</link>
      <description>Hi, &lt;BR /&gt;
&lt;BR /&gt;
I have the following data:&lt;BR /&gt;
&lt;BR /&gt;
sys_id      company_name      company_identifer          state&lt;BR /&gt;
1                   ABC Co.           245679762                      MI&lt;BR /&gt;
2                   XYZ Co.             876234T87                    OH&lt;BR /&gt;
3                   WALDEN          88723O124                    MA&lt;BR /&gt;
4                  GOOGLE          77823I457                      CA&lt;BR /&gt;
5                  TALI                 44321H887                     AZ&lt;BR /&gt;
.&lt;BR /&gt;
.&lt;BR /&gt;
.&lt;BR /&gt;
&lt;BR /&gt;
I have about 10,000 or so obs similar to the ones above. My question is if I need to lookup a company based on the first 6 digits or chars of the company id since I'm not sure about the complete 9 digit, how would I go about implementing that. Also the company_id is passed dynamically rather than hard-coded. &lt;BR /&gt;
&lt;BR /&gt;
So you have a flat file that has the following ids:&lt;BR /&gt;
&lt;BR /&gt;
245679&lt;BR /&gt;
876234&lt;BR /&gt;
88723O&lt;BR /&gt;
77823I&lt;BR /&gt;
44321H&lt;BR /&gt;
&lt;BR /&gt;
These ids are passed on to a macro var where the program then separates the list and takes one id at a time and do a comparison against the dataset. So if the first 6 digits or chars matches the ID then output the obs.&lt;BR /&gt;
&lt;BR /&gt;
I know this can be done by implementing something like:&lt;BR /&gt;
&lt;BR /&gt;
if company_identifier =: &amp;amp;6_digit_id then output;&lt;BR /&gt;
&lt;BR /&gt;
I was only able to make this work for 1 id. However when I code in a do loop I only get the last ID (which is obvious) and I cant seem to find a way to retain the previous IDs that have been passed in. How do I modify the code so I can have it implemented for multiple ID's such that the incoming id value of 6 digits or chars is compared against the datasets 9 digit ID and if the first 6 matches then output those obs. &lt;BR /&gt;
&lt;BR /&gt;
Something like: &lt;BR /&gt;
%main macro;&lt;BR /&gt;
....&lt;BR /&gt;
...&lt;BR /&gt;
&lt;BR /&gt;
data xyz;&lt;BR /&gt;
 %do i = 1 to %count(&amp;amp;id);&lt;BR /&gt;
  %let q = %scan(&amp;amp;id,i);&lt;BR /&gt;
    if company_identifier =: &amp;amp;id then output;&lt;BR /&gt;
....&lt;BR /&gt;
....&lt;BR /&gt;
...&lt;BR /&gt;
 end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
I'm not sure what I'm missing. Your help is much appreciated. &lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Joe</description>
      <pubDate>Tue, 19 Jan 2010 18:29:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42962#M8785</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-01-19T18:29:18Z</dc:date>
    </item>
    <item>
      <title>Re: retaining values in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42963#M8786</link>
      <description>How about &lt;BR /&gt;
if substr(company_identifer , 1, 6) in ( &amp;amp;id ) then output;&lt;BR /&gt;
&lt;BR /&gt;
just create your list for ID quoted with a comma delimiter.</description>
      <pubDate>Tue, 19 Jan 2010 18:40:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42963#M8786</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2010-01-19T18:40:50Z</dc:date>
    </item>
    <item>
      <title>Re: retaining values in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42964#M8787</link>
      <description>Or just for fun.&lt;BR /&gt;
&lt;BR /&gt;
data start;&lt;BR /&gt;
input&lt;BR /&gt;
sys_id  company_name $ company_identifer $  state $;&lt;BR /&gt;
cards;&lt;BR /&gt;
1 ABC  245679762 MI&lt;BR /&gt;
2 XYZ  876234T87 OH&lt;BR /&gt;
3 WALDEN 88723O124 MA&lt;BR /&gt;
4 GOOGLE 77823I457 CA&lt;BR /&gt;
5 TALI 44321H887 AZ&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data look1 (index = (frst6));&lt;BR /&gt;
input frst6 $;&lt;BR /&gt;
cards;&lt;BR /&gt;
245679&lt;BR /&gt;
88723O&lt;BR /&gt;
77823I&lt;BR /&gt;
44321H&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data end;&lt;BR /&gt;
set start;&lt;BR /&gt;
drop frst6;&lt;BR /&gt;
frst6 = substr(company_identifer, 1,6);&lt;BR /&gt;
set look1 key = frst6/unique;&lt;BR /&gt;
 if (not _iorc_ =  %sysrc(_sok)) then do;&lt;BR /&gt;
     delete;&lt;BR /&gt;
     _error_ = 0;&lt;BR /&gt;
  end;&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 19 Jan 2010 18:59:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42964#M8787</guid>
      <dc:creator>Flip</dc:creator>
      <dc:date>2010-01-19T18:59:05Z</dc:date>
    </item>
    <item>
      <title>Re: retaining values in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42965#M8788</link>
      <description>Hi, &lt;BR /&gt;
&lt;BR /&gt;
Thank you so much.. the first solution with substr seems a better and a more simple approach.. &lt;BR /&gt;
&lt;BR /&gt;
I'm gonna try with the substr .. hopefully that should resolve it...</description>
      <pubDate>Tue, 19 Jan 2010 19:33:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42965#M8788</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-01-19T19:33:52Z</dc:date>
    </item>
    <item>
      <title>Re: retaining values in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42966#M8789</link>
      <description>As compared to IF/THEN with the IN operator, you should also consider the performance benefit with using a PROC FORMAT and loading it with the CNTLIN= option, presuming you don't have any string-prefix duplicates.  The DATA step code then would have a single line assignment statement with a PUT function reference.  Fewer moving parts and no macro variable coding necessary.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Tue, 19 Jan 2010 20:20:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42966#M8789</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-01-19T20:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: retaining values in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42967#M8790</link>
      <description>And, in addition to all the other suggestions, if all you want is an output file of matches, then depending on the sizes involved, either a DATA Step MERGE or an SQL JOIN would also produce the desired output without macro code involvement.&lt;BR /&gt;
 &lt;BR /&gt;
I don't actually see much need for macro coding as you have described your data and end result.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Tue, 19 Jan 2010 20:54:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42967#M8790</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-01-19T20:54:37Z</dc:date>
    </item>
    <item>
      <title>Re: retaining values in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42968#M8791</link>
      <description>Yep, agree with Cynthia.&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
create table OUTDATA as&lt;BR /&gt;
select a.* from DATA as a, LOOKUP as b&lt;BR /&gt;
where b.COMPANY_IDENTIFIER like cats(a.COMPANY_IDENTIFIER,'%');&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Or you could also use the substr function to do the match.&lt;BR /&gt;
[pre]&lt;BR /&gt;
where a.COMPANY_IDENTIFIER eq substr(b.COMPANY_IDENTIFIER,1,6);&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;</description>
      <pubDate>Wed, 20 Jan 2010 08:53:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42968#M8791</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2010-01-20T08:53:07Z</dc:date>
    </item>
    <item>
      <title>Re: retaining values in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42969#M8792</link>
      <description>Hi Cynthia,&lt;BR /&gt;
&lt;BR /&gt;
Thanks a bunch for the input. This helped me a lot.&lt;BR /&gt;
&lt;BR /&gt;
Many thanks</description>
      <pubDate>Wed, 20 Jan 2010 15:35:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42969#M8792</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-01-20T15:35:36Z</dc:date>
    </item>
    <item>
      <title>Re: retaining values in a %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42970#M8793</link>
      <description>Hi Santos,&lt;BR /&gt;
&lt;BR /&gt;
This is awesome. Thanks a million for your help. Implementing the proc sql step has proved to me much efficient for me. &lt;BR /&gt;
&lt;BR /&gt;
Once again many many thanks to you and Cynthia, and all you guys. &lt;BR /&gt;
&lt;BR /&gt;
You guys are absolutely fantastically, awesome.</description>
      <pubDate>Wed, 20 Jan 2010 15:37:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/retaining-values-in-a-do-loop/m-p/42970#M8793</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-01-20T15:37:15Z</dc:date>
    </item>
  </channel>
</rss>

