<?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 Nested Do Loop Terminating After 1 Row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Nested-Do-Loop-Terminating-After-1-Row/m-p/785918#M250871</link>
    <description>&lt;P&gt;I'm trying to create a new column for each code I'm looking for. Then flag each row that contain those codes.&lt;/P&gt;&lt;P&gt;The code works for 1 row then terminates. How do I keep looping through each row of my data set?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
  input id$ code1$ code2$ code3$ code4$;
  cards;
aa 03f 06x 01a 05a
bb 05n 02b 01a .
cc 02b . . .
dd 01a 02b . .
;
run;

data top_codes;
  input top_code1$ top_code2$ top_code3$;
  cards;
01d 01a 02b
;
run;

data want (drop= i k);
	
	if _n_ eq 1 then;
		do;
			set top_codes;
			retain _all;
			array tc(*) top_code1--top_code3;
		end;

	set have;

	array _vars(*) code1--code4;
	array code_{3}; 
 	
	do i=1 to dim(_vars);
		do k = 1 to dim(code_);
			if _vars(i) = tc(k) then code_{k} = 1 ;
		end;
	end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 930px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66730i560F027A426D67B1/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This is a image of what I'm getting. I'm expecting to get a line for each row of my data&lt;/P&gt;</description>
    <pubDate>Tue, 14 Dec 2021 00:39:16 GMT</pubDate>
    <dc:creator>cobba</dc:creator>
    <dc:date>2021-12-14T00:39:16Z</dc:date>
    <item>
      <title>Nested Do Loop Terminating After 1 Row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-Do-Loop-Terminating-After-1-Row/m-p/785918#M250871</link>
      <description>&lt;P&gt;I'm trying to create a new column for each code I'm looking for. Then flag each row that contain those codes.&lt;/P&gt;&lt;P&gt;The code works for 1 row then terminates. How do I keep looping through each row of my data set?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
  input id$ code1$ code2$ code3$ code4$;
  cards;
aa 03f 06x 01a 05a
bb 05n 02b 01a .
cc 02b . . .
dd 01a 02b . .
;
run;

data top_codes;
  input top_code1$ top_code2$ top_code3$;
  cards;
01d 01a 02b
;
run;

data want (drop= i k);
	
	if _n_ eq 1 then;
		do;
			set top_codes;
			retain _all;
			array tc(*) top_code1--top_code3;
		end;

	set have;

	array _vars(*) code1--code4;
	array code_{3}; 
 	
	do i=1 to dim(_vars);
		do k = 1 to dim(code_);
			if _vars(i) = tc(k) then code_{k} = 1 ;
		end;
	end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 930px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66730i560F027A426D67B1/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This is a image of what I'm getting. I'm expecting to get a line for each row of my data&lt;/P&gt;</description>
      <pubDate>Tue, 14 Dec 2021 00:39:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-Do-Loop-Terminating-After-1-Row/m-p/785918#M250871</guid>
      <dc:creator>cobba</dc:creator>
      <dc:date>2021-12-14T00:39:16Z</dc:date>
    </item>
    <item>
      <title>Re: Nested Do Loop Terminating After 1 Row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-Do-Loop-Terminating-After-1-Row/m-p/785922#M250872</link>
      <description>&lt;P&gt;How about providing a more complete example of what you expect. I am not all sure of what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: You are getting one row of data for each line of date in the Top_codes data set. Multiple Set statements are a complex subject.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your goal is to have every record from Have matched to the Top_code data set then you want a Cartesian product such as&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as
   select a.*,b.*
   from top_codes as a, have as b
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;If your top_codes had two rows of data you have every row of top_codes joined with every row of Have yielding 2 times as many records as Have.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Dec 2021 00:48:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-Do-Loop-Terminating-After-1-Row/m-p/785922#M250872</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-12-14T00:48:43Z</dc:date>
    </item>
    <item>
      <title>Re: Nested Do Loop Terminating After 1 Row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Nested-Do-Loop-Terminating-After-1-Row/m-p/785923#M250873</link>
      <description>&lt;P&gt;You have an extra semi-colon.&lt;/P&gt;
&lt;P&gt;Yet another reason to dislike that indentation style. Keep the DO on the end of the IF line and align the END with the IF&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop= i k);

  if _n_ eq 1 then do;
    set top_codes;
    retain _all;
    array tc(*) top_code1--top_code3;
  end;

  set have;

  array _vars(*) code1--code4;
  array code_{3}; 

  do i=1 to dim(_vars);
    do k = 1 to dim(code_);
      if _vars(i) = tc(k) then code_{k} = 1 ;
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You don't even need a DO/END block at that point because the SET statement is the only executable statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if _n_ eq 1 then set top_codes;

  retain _all;
  array tc(*) top_code1--top_code3;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not sure what the RETAIN statement is doing. You are not creating any variable named _ALL.&amp;nbsp; And if there is a variable named _ALL coming in from one of those two datasets it would already be retained so no need to list it in a RETAIN statement.&amp;nbsp; If you meant to use the _ALL_ variable list then that also would do nothing as the only variables defined at that point are the ones read from TOP_CODES and those are already set to be retained.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Dec 2021 01:13:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Nested-Do-Loop-Terminating-After-1-Row/m-p/785923#M250873</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-14T01:13:29Z</dc:date>
    </item>
  </channel>
</rss>

