<?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: Do Loop or If Then? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-or-If-Then/m-p/362878#M85779</link>
    <description>&lt;P&gt;Since you already have a SAS data set, I have to assume you already know how many months you have.&amp;nbsp; It's a simple application for arrays (if anything using arrays can actually be considered simple):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array months {60} month1-month60;&lt;/P&gt;
&lt;P&gt;do i=1 to number_of_obs;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if months{i}=. then months{i}=0;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm using 60 as the total number of months, but you have to plug in the right number there.&lt;/P&gt;</description>
    <pubDate>Tue, 30 May 2017 21:01:13 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-05-30T21:01:13Z</dc:date>
    <item>
      <title>Do Loop or If Then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-or-If-Then/m-p/362871#M85776</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;First time poster, so hello everyone!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data step that I am having a very difficult time with. I have a data set that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Data Set:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Number of Obs&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp; Month 1 |&amp;nbsp; Month 2 |&amp;nbsp; Month 3 |&amp;nbsp; Month 4 |&amp;nbsp; Month 5 |&amp;nbsp; Month 6 | ... | Month X|&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; |&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; 50&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; . &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; . &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; |&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; |&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; 50&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; . &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; . &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; |&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Desired Output:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Number of Obs&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp; Month 1 |&amp;nbsp; Month 2 |&amp;nbsp; Month 3 |&amp;nbsp; Month 4 |&amp;nbsp; Month 5 |&amp;nbsp; Month 6 | ... | Month X|&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; |&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; 50&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0 &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; |&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; |&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp; 50&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0 &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; |&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically, for every row:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;If the Month is less than the Number of Obs, I need to impute a 0 and if the Month is greater than the Number of Obs, I need to leave the value missing.&lt;UL&gt;&lt;LI&gt;So, in the first row, I need to drop in a 0 in Month 3 and leave Months 5 - Months X untouched.&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;In Excel, I could put in a formula that says if (Month1 &amp;lt;= Number of Obs, Number, 0) and drag the formula but in SAS I cannot get this to work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone know how to do this?&amp;nbsp; I am driving myself crazy over this, so any help would be appreciated.&amp;nbsp; I can't hard code this all manually because I will have about a hundred months of columns.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!!&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2017 20:49:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-or-If-Then/m-p/362871#M85776</guid>
      <dc:creator>thrice1984</dc:creator>
      <dc:date>2017-05-30T20:49:44Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop or If Then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-or-If-Then/m-p/362878#M85779</link>
      <description>&lt;P&gt;Since you already have a SAS data set, I have to assume you already know how many months you have.&amp;nbsp; It's a simple application for arrays (if anything using arrays can actually be considered simple):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array months {60} month1-month60;&lt;/P&gt;
&lt;P&gt;do i=1 to number_of_obs;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if months{i}=. then months{i}=0;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm using 60 as the total number of months, but you have to plug in the right number there.&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2017 21:01:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-or-If-Then/m-p/362878#M85779</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-05-30T21:01:13Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop or If Then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-or-If-Then/m-p/362884#M85781</link>
      <description>&lt;P&gt;That worked perfectly, thank you so much for the time.&amp;nbsp; I'm sorry if that was a stupid question.&amp;nbsp; Your solution was very elegant and I appreciate that greatly!&lt;/P&gt;</description>
      <pubDate>Tue, 30 May 2017 21:13:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-or-If-Then/m-p/362884#M85781</guid>
      <dc:creator>thrice1984</dc:creator>
      <dc:date>2017-05-30T21:13:46Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop or If Then?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-or-If-Then/m-p/362960#M85810</link>
      <description>&lt;P&gt;A question can only be stupid if it is stated in a stupid way. Your question gave a clear picture of your situation, and what you wanted to achieve, so - voila! - &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; was able to quickly come up with a solution for you. So your question was a good question typical for a beginner, and it was one of those that are a pleasure to answer. Such questions can never be considered "stupid"!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just one hint for the future: it helps &lt;U&gt;us&lt;/U&gt; greatly when example data is provided in a way that makes it easy for us to recreate your datasets quickly and independent from the environment we use. So we like to present data in a data step, which might have looked like that in your case:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input number_of_obs month1 month2 month3 month4 month5 month6;
cards;
4 50 25 . 10 . .
5 50 25 . . 10 .
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Just mastering the creation of a dataset with a data step like this provides a learning experience on its own (it was for me).&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2017 08:06:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-or-If-Then/m-p/362960#M85810</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-05-31T08:06:49Z</dc:date>
    </item>
  </channel>
</rss>

