<?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: SAS loop through observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270473#M53752</link>
    <description>&lt;P&gt;I am pretty sure that ARRAYs are covered in the documentation.&lt;/P&gt;
&lt;P&gt;But perhaps you meant the DO OVER?&lt;/P&gt;
&lt;P&gt;I find it much each to explain this type of situation by using a concept that matches the application. In this problem the "INDEX" that is required if you do not use the DO OVER construct has no meaning. &amp;nbsp;The purpose of the DO loop is apply the same logic to EVERY member of the array. The relative order of the members of the array is meaningless.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Now if the variables represented something where an index makes sense then it is clearer to use index notation. So if the readings represent results for the months of the year it would make more sense to write:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array reading m1-m12 ;
do month=1 to 12 ;
   if reading(month)= 999 then reading(month)=X;
end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 14 May 2016 01:31:06 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2016-05-14T01:31:06Z</dc:date>
    <item>
      <title>SAS loop through observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270216#M53655</link>
      <description>&lt;P&gt;Hi all, I am a new user of SAS. I have a data and I want to write a loop throug observations, but I don't know how to do it in SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the data&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt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gt;2&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;230&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;999&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to write a loop that&amp;nbsp;from the fisrt subject to the last subject, if this subject's kn1-4 equals to 999, then change the 999 to the value of this subject's kn.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much!&lt;/P&gt;</description>
      <pubDate>Thu, 12 May 2016 21:34:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270216#M53655</guid>
      <dc:creator>Roxy_Dong</dc:creator>
      <dc:date>2016-05-12T21:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop through observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270227#M53657</link>
      <description>A SAS data step loops through observations automatically.&lt;BR /&gt;For you rule use simple if-then-assignment statements. &lt;BR /&gt;I recommend that you start going through some initial training / read books and concepts parts of Base SAS user guides.</description>
      <pubDate>Thu, 12 May 2016 22:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270227#M53657</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-05-12T22:33:58Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop through observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270231#M53658</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Thank you for replying me.&lt;/P&gt;&lt;P&gt;I know how to use if... then, but in&amp;nbsp;my original data I have hundreds observations and more than 30 variables and each one's kn value is different.&amp;nbsp;If I use &amp;nbsp;kn1=999 then kn1= kn, it will be pretty fussy. I tried array but it treat data in column.&lt;/P&gt;</description>
      <pubDate>Thu, 12 May 2016 22:45:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270231#M53658</guid>
      <dc:creator>Roxy_Dong</dc:creator>
      <dc:date>2016-05-12T22:45:20Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop through observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270232#M53659</link>
      <description>&lt;P&gt;Here is a snippet of code you may find useful&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; array k Kn1-Kn4;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; do i= 1 to dim(k);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if k[i]=999 then k[i]= kn;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; drop i;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 May 2016 22:47:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270232#M53659</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-05-12T22:47:49Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop through observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270238#M53662</link>
      <description>&lt;P&gt;Unless your problem is different than it sounds you do NOT want to loop through observations. &amp;nbsp;SAS does that for you already. &amp;nbsp;So let's just consider how to handle a single observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ID	KN1	KN2	KN3	KN4	KN
103	1	3	4	999	2&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you wrote the code by hand you would do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if KN1=999 then KN1=KN;
if KN2=999 then KN2=KN;
if KN3=999 then KN3=KN;
if KN4=999 then KN4=KN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can use an ARRAY to allow you to apply to same rule to all of the similar variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array _KN KN1-KN4 ;
do over _KN;
  if _KN=999 then _KN=KN ;
end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To do more variables just add them to the array definition.&lt;/P&gt;</description>
      <pubDate>Thu, 12 May 2016 23:51:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270238#M53662</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-05-12T23:51:18Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop through observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270257#M53669</link>
      <description>&lt;P&gt;Nice explanation &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom﻿&lt;/a&gt;. But with all due respect, why teach an undocumented feature of the data step language to a beginner?&lt;/P&gt;</description>
      <pubDate>Fri, 13 May 2016 03:56:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270257#M53669</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-05-13T03:56:54Z</dc:date>
    </item>
    <item>
      <title>Re: SAS loop through observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270473#M53752</link>
      <description>&lt;P&gt;I am pretty sure that ARRAYs are covered in the documentation.&lt;/P&gt;
&lt;P&gt;But perhaps you meant the DO OVER?&lt;/P&gt;
&lt;P&gt;I find it much each to explain this type of situation by using a concept that matches the application. In this problem the "INDEX" that is required if you do not use the DO OVER construct has no meaning. &amp;nbsp;The purpose of the DO loop is apply the same logic to EVERY member of the array. The relative order of the members of the array is meaningless.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Now if the variables represented something where an index makes sense then it is clearer to use index notation. So if the readings represent results for the months of the year it would make more sense to write:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array reading m1-m12 ;
do month=1 to 12 ;
   if reading(month)= 999 then reading(month)=X;
end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 May 2016 01:31:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-loop-through-observations/m-p/270473#M53752</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-05-14T01:31:06Z</dc:date>
    </item>
  </channel>
</rss>

