<?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: Looping through observations and variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488585#M287422</link>
    <description>&lt;P&gt;Sorry too late, was sleeping.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming i understood your requirement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Hierarchy;
infile datalines dlm='' truncover;
length CustID $10 L1 $20 L2 $20 L3 $20 L4 $20 L5 $20 L6 $20 L7 $20 L8 $20;
input CustID  $ L1 $ L2 $ L3 $ L4 $ L5 $ L6 $  L7 $ L8 $;
cards;
1 A20 A30 A40 A50 A60 A70 A80
2 A40 A70 A80 A90
3 A70 A80 A90 A100
4 A30 A60 A70
;
run;
data want;
do n=1 by 1 until(l);
	set Hierarchy end=l;
	array t(*) l8-l1;
	array j(100) $ _temporary_;
	j(n)=coalescec(of t(*));
end;
	l=0;
do until(l);
	set Hierarchy end=l;
	do _n_=1 to dim(t);
	if not missing(t(_n_)) and t(_n_) in j then t(_n_)=cats('XXX',t(_n_));
	end;
	output;
end;
keep custid l:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Aug 2018 14:30:28 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-08-21T14:30:28Z</dc:date>
    <item>
      <title>Looping through observations and variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488491#M287414</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the below data set (forgive me for the arrow, it will make sense later)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="loop1.PNG" style="width: 395px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22654iB1FE7081B2DC1CFA/image-size/large?v=v2&amp;amp;px=999" role="button" title="loop1.PNG" alt="loop1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This can be created with the below code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Hierarchy;
 INFILE DATALINES DLM='' missover;
 length CustID $10 L1 $20 L2 $20 L3 $20 L4 $20 L5 $20 L6 $20 L7 $20 L8 $20;
 input CustID  $ L1 $ L2 $ L3 $ L4 $ L5 $ L6 $  L7 $ L8 $;
 cards;
 1 A20 A30 A40 A50 A60 A70 A80
 2 A40 A70 A80 A90
 3 A70 A80 A90 A100
 4 A30 A60 A70
 ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;TASKS:&lt;BR /&gt;My tasks are:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. First, grab the last variable value of each row and prefix it with a 'XXX' as shown in the diagram. So for observation 1, L7 will be&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; XXXA80.&lt;/P&gt;&lt;P&gt;2. Then (using row one as an example), loop through every other row and where the value A80 (last value of row one) is found, replace it with XXXA80.&lt;/P&gt;&lt;P&gt;Another example of step 2: For observation 4, the last value is A70 (which will be replaced with XXXA70 as in step 1). Wherever A70 is found in the dataset e.g. in observation 1, replace it with XXXA70.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EXPECTED OUTCOME:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="loop2.PNG" style="width: 464px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22655i688987C842315A33/image-size/large?v=v2&amp;amp;px=999" role="button" title="loop2.PNG" alt="loop2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 10:47:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488491#M287414</guid>
      <dc:creator>frupaul</dc:creator>
      <dc:date>2018-08-21T10:47:16Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through observations and variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488493#M287415</link>
      <description>&lt;OL&gt;
&lt;LI&gt;transpose into a long format.&lt;/LI&gt;
&lt;LI&gt;create a second dataset where only the last for every ID is kept&lt;/LI&gt;
&lt;LI&gt;create a format from that (that prepends the XXX), where all other values remain unchanged&lt;/LI&gt;
&lt;LI&gt;apply that format to the dataset&lt;/LI&gt;
&lt;LI&gt;if needed, transpose back to wide format&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 10:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488493#M287415</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-21T10:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through observations and variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488502#M287416</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134464"&gt;@frupaul&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=a i);
length a $20;
dcl hash h();
h.definekey('a');
h.definedone();
a=' ';

do until(eof1);
  set hierarchy end=eof1;
  array l[8];
  do i=8 to 1 by -1 while(l[i]=' ');
  end;
  if i then h.ref(key: l[i], data: l[i]);
end;

do until(eof2);
  set hierarchy end=eof2;
  do i=1 to 8;
    if h.check(key: l[i])=0 then l[i]='XXX'||l[i];
  end;
  output;
end;

stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please note that the result does &lt;EM&gt;not&lt;/EM&gt; match your "expected outcome" because it &lt;EM&gt;does&lt;/EM&gt; meet&amp;nbsp;your requirement "&lt;SPAN&gt;Wherever A70 is found in the dataset e.g. in observation 1, replace it with XXXA70".&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Edit: Replaced &lt;FONT face="courier new,courier"&gt;find&lt;/FONT&gt; with &lt;FONT face="courier new,courier"&gt;check&lt;/FONT&gt;, which should be a bit faster.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 11:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488502#M287416</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-08-21T11:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through observations and variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488515#M287417</link>
      <description>&lt;P&gt;You can also use arrays:&lt;/P&gt;
&lt;PRE&gt;data want;
  set hierachy;
  array l{20};
  first=0;
  do i=20 to 1 step -1;
    if first=0 and l{i} ne "" then do;
      first=1;
      l{i}=cats("XXX",l{i});
    end;
    else if l{i} in ("A70","A80") then l{i}=cats("XXX",l{i});
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Do note that as per&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;, a long structure with few columns and more obs would be a better data structure for this and other programming.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 12:22:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488515#M287417</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-21T12:22:45Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through observations and variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488518#M287418</link>
      <description>&lt;P&gt;I have now played around with your data a little, and implemented my suggestion:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Hierarchy;
infile datalines dlm='' truncover;
length CustID $10 L1 $20 L2 $20 L3 $20 L4 $20 L5 $20 L6 $20 L7 $20 L8 $20;
input CustID  $ L1 $ L2 $ L3 $ L4 $ L5 $ L6 $  L7 $ L8 $;
cards;
1 A20 A30 A40 A50 A60 A70 A80
2 A40 A70 A80 A90
3 A70 A80 A90 A100
4 A30 A60 A70
;
run;

/* transpose */
proc transpose
  data=hierarchy
  out=trans (where=(col1 ne ''))
;
by custid;
var l:;
run;

/* create a format */
data cntlin;
set trans (rename=(col1=start)) end=eof;
by custid;
if last.custid;
label = 'XXX' !! start;
type = 'C';
fmtname = 'convert';
keep fmtname type start label;
run;

proc sort data=cntlin nodupkey;
by start;
run;

proc format library=work cntlin=cntlin;
run;

/* create wanted dataset in long format */
data want;
set trans;
col1 = put(col1,convert.);
run;

/* re-transpose to wide format */
proc transpose data=want out=want1;
by custid;
var col1;
id _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the code is quite simple, and makes no assumptions apart from the variable names starting with 'L'. Otherwise, it is completely data-driven.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 12:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488518#M287418</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-21T12:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through observations and variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488523#M287419</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;: Just a minor remark: I think variable L8 gets lost in the PROC TRANSPOSE step because it has only missing values in the test data. As a result, dataset WANT1 doesn't contain it.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 12:48:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488523#M287419</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-08-21T12:48:43Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through observations and variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488524#M287420</link>
      <description>&lt;P&gt;If you want to give a try here is another way,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Hierarchy;
 INFILE DATALINES DLM='' missover;
 length CustID $10 L1 $20 L2 $20 L3 $20 L4 $20 L5 $20 L6 $20 L7 $20 L8 $20;
 input CustID  $ L1 $ L2 $ L3 $ L4 $ L5 $ L6 $  L7 $ L8 $;
 cards;
 1 A20 A30 A40 A50 A60 A70 A80
 2 A40 A70 A80 A90
 3 A70 A80 A90 A100
 4 A30 A60 A70
 ;
 run;
data test(keep=L_);
format L_ $50.;
set Hierarchy;
L_=scan(strip(CATX(' ',of L:)),-1);
run;

proc sql noprint;
select distinct quote(strip(L_)) into: L_ separated by ","
from test;
quit;
%put &amp;amp;L_;

data want;
set Hierarchy;
array All_L{*} L:;
do i=1 to dim(All_L);
if strip(All_L(i)) in (&amp;amp;L_) then All_L(i)=cats('XXX',Strip(All_L(i)));
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Aug 2018 12:53:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488524#M287420</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-08-21T12:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through observations and variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488525#M287421</link>
      <description>&lt;P&gt;Correct. That can be fixed by adding a final step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data final;
set
  hierarchy (obs=0)
  want1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I view the dropping of unused columns as a feature &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 12:53:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488525#M287421</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-21T12:53:44Z</dc:date>
    </item>
    <item>
      <title>Re: Looping through observations and variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488585#M287422</link>
      <description>&lt;P&gt;Sorry too late, was sleeping.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming i understood your requirement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Hierarchy;
infile datalines dlm='' truncover;
length CustID $10 L1 $20 L2 $20 L3 $20 L4 $20 L5 $20 L6 $20 L7 $20 L8 $20;
input CustID  $ L1 $ L2 $ L3 $ L4 $ L5 $ L6 $  L7 $ L8 $;
cards;
1 A20 A30 A40 A50 A60 A70 A80
2 A40 A70 A80 A90
3 A70 A80 A90 A100
4 A30 A60 A70
;
run;
data want;
do n=1 by 1 until(l);
	set Hierarchy end=l;
	array t(*) l8-l1;
	array j(100) $ _temporary_;
	j(n)=coalescec(of t(*));
end;
	l=0;
do until(l);
	set Hierarchy end=l;
	do _n_=1 to dim(t);
	if not missing(t(_n_)) and t(_n_) in j then t(_n_)=cats('XXX',t(_n_));
	end;
	output;
end;
keep custid l:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 14:30:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-through-observations-and-variables/m-p/488585#M287422</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-08-21T14:30:28Z</dc:date>
    </item>
  </channel>
</rss>

