<?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: Create variable in do loop in data step? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436050#M108398</link>
    <description>exactly, why do statement comes in between data and set? how to master 'do' statement systematically? from the beginning</description>
    <pubDate>Sun, 11 Feb 2018 03:21:15 GMT</pubDate>
    <dc:creator>Cruise</dc:creator>
    <dc:date>2018-02-11T03:21:15Z</dc:date>
    <item>
      <title>Create variable in do loop in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436046#M108395</link>
      <description>&lt;P&gt;Is it possible to create a variable 'risk' in data step for the range from 0.1 thru 2 by 0.1 increment where i=20.&lt;BR /&gt;However, code below runs non-stop.&lt;BR /&gt;Any suggestion?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
input change;
datalines;
change01
change02
change03
change04
change05
change06
change07
change08
change09
change10
change11
change12
change13
change14
change15
change16
change17
change18
change19
change20
;

data temp1; set temp;
do risk = 0.1 to 2 by 0.1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 11 Feb 2018 03:10:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436046#M108395</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-02-11T03:10:53Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable in do loop in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436048#M108397</link>
      <description>&lt;P&gt;do you mean this?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
input change $10.;
datalines;
change01
change02
change03
change04
change05
change06
change07
change08
change09
change10
change11
change12
change13
change14
change15
change16
change17
change18
change19
change20
;

data want;
do risk=0.1 to 2 by 0.1 until(last);
set temp end=last;
output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 11 Feb 2018 03:15:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436048#M108397</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-02-11T03:15:40Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable in do loop in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436050#M108398</link>
      <description>exactly, why do statement comes in between data and set? how to master 'do' statement systematically? from the beginning</description>
      <pubDate>Sun, 11 Feb 2018 03:21:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436050#M108398</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-02-11T03:21:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable in do loop in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436051#M108399</link>
      <description>&lt;P&gt;I learned most of SAS by reading the book "Learning SAS by example" by author ron cody and a book by Art carpenter. If you could get those two books, read page by page, you will be coding here and helping others. HTH&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PS Those books have all those you need. Trust me&lt;/P&gt;</description>
      <pubDate>Sun, 11 Feb 2018 03:24:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436051#M108399</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-02-11T03:24:20Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable in do loop in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436053#M108401</link>
      <description>wow, what's the title for book by Art Carpenter? I googled, one popped up for macro</description>
      <pubDate>Sun, 11 Feb 2018 03:25:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436053#M108401</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-02-11T03:25:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable in do loop in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436054#M108402</link>
      <description>&lt;P&gt;&lt;IMG src="https://books.google.com/books/content/images/frontcover/pqBC4tz9CDwC?fife=w200-h300" border="0" alt="Cover art" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13683"&gt;@ArtC&lt;/a&gt;.....This book will make anybody a master&lt;/P&gt;</description>
      <pubDate>Sun, 11 Feb 2018 03:30:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436054#M108402</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-02-11T03:30:57Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable in do loop in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436057#M108404</link>
      <description>wow, i shall own this piece. Thanks a lot!</description>
      <pubDate>Sun, 11 Feb 2018 03:32:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436057#M108404</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-02-11T03:32:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable in do loop in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436058#M108405</link>
      <description>Last question please. Which one would you recommend me to start with?</description>
      <pubDate>Sun, 11 Feb 2018 03:35:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436058#M108405</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-02-11T03:35:30Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable in do loop in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436059#M108406</link>
      <description>&lt;P&gt;A different way, this doesn't require you to know that you're going up to 20 ahead of time it just increments it until the end.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;No RETAIN statement is required because of the implicit RETAIN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set temp;
    risk + 0.1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Feb 2018 03:37:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436059#M108406</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-11T03:37:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable in do loop in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436060#M108407</link>
      <description>&lt;P&gt;Ok, Follow this idea if you wish, coz that worked for me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. SAS documentation is great for quick reference like a user manual&lt;/P&gt;&lt;P&gt;2. Ron cody is the best book to start. I regard him guru of all beginners&lt;/P&gt;&lt;P&gt;2. Art carpenter is great when you have done with ron cody's&lt;/P&gt;</description>
      <pubDate>Sun, 11 Feb 2018 03:39:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436060#M108407</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-02-11T03:39:00Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable in do loop in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436091#M108424</link>
      <description>wow!</description>
      <pubDate>Sun, 11 Feb 2018 16:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436091#M108424</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-02-11T16:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create variable in do loop in data step?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436092#M108425</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/132289"&gt;@Cruise&lt;/a&gt; wrote:&lt;BR /&gt;exactly, why do statement comes in between data and set? how to master 'do' statement systematically? from the beginning&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Like for most real problems there is no one "right" answer, but you do need to think about what the code you have written is doing.&lt;/P&gt;
&lt;P&gt;So for your original program you tried this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp1;
  set temp;
  do risk = 0.1 to 2 by 0.1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You really don't need to know SAS to understand why that won't work.&amp;nbsp; Your DO loop has nothing in it so all it is doing is setting a constant value to the RISK variable.&amp;nbsp; (you do need to understand SAS to know that it set RISK to 2.1, plus any minor differences cause by adding 21 floating point representations of 0.1 together)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you want each input observation to be repeated 20 times for each value of RISK then you just need to add an OUTPUT statement in the loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If instead you want to values for risk on the first observation to start at 0.1 and increase by 1 for each observation after that then you can retain the value of RISK and increment it. The &lt;A href="http://documentation.sas.com/?docsetId=lestmtsref&amp;amp;docsetTarget=n1dfiqj146yi2cn1maeju9wo7ijs.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;SUM statement&lt;/A&gt; is an easy way to do that.&amp;nbsp; Usually you will see it used to add a counter variable, but you can increment by anything.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp1;
  set temp;
  risk + 0.1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you put the SET statement inside of a DO loop then it will execute multiple times for each iteration of the DATA step.&amp;nbsp; But this also means that the implied OUTPUT that is done at the end of a normal DATA step would only happen after the DO loop ends.&amp;nbsp; So if you do not add an OUTPUT statement in the loop then only every 20th observation would be written.&lt;/P&gt;
&lt;P&gt;Now if you just want the RISK value to increment by 0.1 you do not need to put an upper bound on the DO loop.&amp;nbsp; The data step (like most data steps) will stop when it reads past the end of the input dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp1;
  do risk = 0.1 by 0.1;
    set temp;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Feb 2018 17:22:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-variable-in-do-loop-in-data-step/m-p/436092#M108425</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-02-11T17:22:44Z</dc:date>
    </item>
  </channel>
</rss>

