<?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: How to simplify the code in data step by macro and do loop？ in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-the-code-in-data-step-by-macro-and-do-loop/m-p/443815#M111065</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/107709"&gt;@qiali&lt;/a&gt;&amp;nbsp;if the answer you've received solves your problem please mark it as the solution.&lt;/P&gt;</description>
    <pubDate>Thu, 08 Mar 2018 16:30:05 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-03-08T16:30:05Z</dc:date>
    <item>
      <title>How to simplify the code in data step by macro and do loop？</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-the-code-in-data-step-by-macro-and-do-loop/m-p/443570#M110979</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dataset_b;
set dataset_a;


if year = 1995 then cost = charge*ccr1995;
if year = 1996 then cost = charge*ccr1996;
if year = 1997 then cost = charge*ccr1997;
if year = 1998 then cost = charge*ccr1998;
if year = 1999 then cost = charge*ccr1999;
if year = 2000 then cost = charge*ccr2000;
if year = 2001 then cost = charge*ccr2001;
if year = 2002 then cost = charge*ccr2002;
if year = 2003 then cost = charge*ccr2003;
if year = 2004 then cost = charge*ccr2004;
if year = 2005 then cost = charge*ccr2005;
if year = 2006 then cost = charge*ccr2006;
if year = 2007 then cost = charge*ccr2007;
if year = 2008 then cost = charge*ccr2008;
if year = 2009 then cost = charge*ccr2009;
if year = 2010 then cost = charge*ccr2010;
if year = 2011 then cost = charge*ccr2011;
if year = 2012 then cost = charge*ccr2012;
if year = 2013 then cost = charge*ccr2013;
if year = 2014 then cost = charge*ccr2014;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi All，&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think it is a very basic and easy question about macro and do loop.&amp;nbsp; Could anyone help to&amp;nbsp;simplify it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried some different ways to code. None of them works....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Qian&lt;/P&gt;</description>
      <pubDate>Thu, 08 Mar 2018 00:19:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-the-code-in-data-step-by-macro-and-do-loop/m-p/443570#M110979</guid>
      <dc:creator>qiali</dc:creator>
      <dc:date>2018-03-08T00:19:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify the code in data step by macro and do loop？</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-the-code-in-data-step-by-macro-and-do-loop/m-p/443580#M110982</link>
      <description>&lt;P&gt;This would be a bad application for macros for a simple reason.&amp;nbsp; The code is inefficient, when it fails to use the word ELSE.&amp;nbsp; It evaluates YEAR 20 times per observation, even when a match has already been found.&amp;nbsp; Using macro language would obscure the fact that it is inefficient code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is another approach that still cuts down on the amount of programming, and will be a subject you should learn before worrying about macro language:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data dataset_b;&lt;/P&gt;
&lt;P&gt;set dataset_a;&lt;/P&gt;
&lt;P&gt;array ccr {1995:2014} ccr1995-ccr2014;&lt;/P&gt;
&lt;P&gt;if (1995 &amp;lt;= year &amp;lt;= 2014) then cost = charge * ccr{year};&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Mar 2018 01:09:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-the-code-in-data-step-by-macro-and-do-loop/m-p/443580#M110982</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-08T01:09:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify the code in data step by macro and do loop？</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-the-code-in-data-step-by-macro-and-do-loop/m-p/443581#M110983</link>
      <description>Thank you so much!!!!!!</description>
      <pubDate>Thu, 08 Mar 2018 01:12:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-the-code-in-data-step-by-macro-and-do-loop/m-p/443581#M110983</guid>
      <dc:creator>qiali</dc:creator>
      <dc:date>2018-03-08T01:12:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify the code in data step by macro and do loop？</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-the-code-in-data-step-by-macro-and-do-loop/m-p/443815#M111065</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/107709"&gt;@qiali&lt;/a&gt;&amp;nbsp;if the answer you've received solves your problem please mark it as the solution.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Mar 2018 16:30:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-the-code-in-data-step-by-macro-and-do-loop/m-p/443815#M111065</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-03-08T16:30:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to simplify the code in data step by macro and do loop？</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-the-code-in-data-step-by-macro-and-do-loop/m-p/450492#M113421</link>
      <description>&lt;PRE&gt;Load all of your formulas into a macro variable and quote it so the ';' is deactivated. When it is expanded into your datastep, then the formulas will all be active. We do this to load over 400 formulas from the database and have them active in our programs for calcualtions.

%let myFormulas=%str(if year = 1995 then cost = charge*ccr1995;else;
if year = 1996 then cost = charge*ccr1996;else;
if year = 1997 then cost = charge*ccr1997;else;
if year = 1998 then cost = charge*ccr1998;else;
if year = 1999 then cost = charge*ccr1999;else;
if year = 2000 then cost = charge*ccr2000;else;
if year = 2001 then cost = charge*ccr2001;else;
if year = 2002 then cost = charge*ccr2002;else;
if year = 2003 then cost = charge*ccr2003;else;
if year = 2004 then cost = charge*ccr2004;else;
if year = 2005 then cost = charge*ccr2005;else;
if year = 2006 then cost = charge*ccr2006;else;
if year = 2007 then cost = charge*ccr2007;else;
if year = 2008 then cost = charge*ccr2008;else;
if year = 2009 then cost = charge*ccr2009;else;
if year = 2010 then cost = charge*ccr2010;else;
if year = 2011 then cost = charge*ccr2011;else;
if year = 2012 then cost = charge*ccr2012;else;
if year = 2013 then cost = charge*ccr2013;else;
if year = 2014 then cost = charge*ccr2014;);
data dataset_b;
  set dataset_a;
  &amp;amp;myFormulas.;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 02 Apr 2018 23:19:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-simplify-the-code-in-data-step-by-macro-and-do-loop/m-p/450492#M113421</guid>
      <dc:creator>morgalr</dc:creator>
      <dc:date>2018-04-02T23:19:21Z</dc:date>
    </item>
  </channel>
</rss>

