<?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 Looping in a Datastep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464730#M285121</link>
    <description>&lt;P&gt;Suppose I have the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data EGTASK.ASIA_GRP_GC_WITHIN_TMP;
	set EGTASK.ASIA_GRP_WCUTOT_TMP2;

		/* Calculate Growth from Same Month in Previous Year, and Contribution to Growth of Group and Total */

		WCU_TOT_ASIAGRP_G=WCU_TOT_ASIAGRP/lag12(WCU_TOT_ASIAGRP)-1;
			WCU_CGASIA_N01144=(WCU_TOT_N01144-lag12(WCU_TOT_N01144))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N01874=(WCU_TOT_N01874-lag12(WCU_TOT_N01874))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N00369=(WCU_TOT_N00369-lag12(WCU_TOT_N00369))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N00274=(WCU_TOT_N00274-lag12(WCU_TOT_N00274))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N01870=(WCU_TOT_N01870-lag12(WCU_TOT_N01870))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N00346=(WCU_TOT_N00346-lag12(WCU_TOT_N00346))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N01305=(WCU_TOT_N01305-lag12(WCU_TOT_N01305))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N00459=(WCU_TOT_N00459-lag12(WCU_TOT_N00459))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N01296=(WCU_TOT_N01296-lag12(WCU_TOT_N01296))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N00462=(WCU_TOT_N00462-lag12(WCU_TOT_N00462))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N02229=(WCU_TOT_N02229-lag12(WCU_TOT_N02229))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N01893=(WCU_TOT_N01893-lag12(WCU_TOT_N01893))/lag12(WCU_TOT_ASIAGRP);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to clean this up by looping based on a client ID list? I know this can be done in a macro, just not sure if it can be done in a datastep.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for example (pseudo code):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;client_list="N01144 N01874 N00369"&lt;/P&gt;&lt;P&gt;loop&amp;nbsp;client_list&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WCU_CGASIA_&amp;amp;client_list=(WCU_TOT_&amp;amp;client_list-lag12(WCU_TOT_&amp;amp;client_list))/lag12(WCU_TOT_ASIAGRP);&lt;/P&gt;&lt;P&gt;endloop&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thx&lt;/P&gt;</description>
    <pubDate>Thu, 24 May 2018 13:03:35 GMT</pubDate>
    <dc:creator>BCNAV</dc:creator>
    <dc:date>2018-05-24T13:03:35Z</dc:date>
    <item>
      <title>Looping in a Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464730#M285121</link>
      <description>&lt;P&gt;Suppose I have the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data EGTASK.ASIA_GRP_GC_WITHIN_TMP;
	set EGTASK.ASIA_GRP_WCUTOT_TMP2;

		/* Calculate Growth from Same Month in Previous Year, and Contribution to Growth of Group and Total */

		WCU_TOT_ASIAGRP_G=WCU_TOT_ASIAGRP/lag12(WCU_TOT_ASIAGRP)-1;
			WCU_CGASIA_N01144=(WCU_TOT_N01144-lag12(WCU_TOT_N01144))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N01874=(WCU_TOT_N01874-lag12(WCU_TOT_N01874))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N00369=(WCU_TOT_N00369-lag12(WCU_TOT_N00369))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N00274=(WCU_TOT_N00274-lag12(WCU_TOT_N00274))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N01870=(WCU_TOT_N01870-lag12(WCU_TOT_N01870))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N00346=(WCU_TOT_N00346-lag12(WCU_TOT_N00346))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N01305=(WCU_TOT_N01305-lag12(WCU_TOT_N01305))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N00459=(WCU_TOT_N00459-lag12(WCU_TOT_N00459))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N01296=(WCU_TOT_N01296-lag12(WCU_TOT_N01296))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N00462=(WCU_TOT_N00462-lag12(WCU_TOT_N00462))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N02229=(WCU_TOT_N02229-lag12(WCU_TOT_N02229))/lag12(WCU_TOT_ASIAGRP);
			WCU_CGASIA_N01893=(WCU_TOT_N01893-lag12(WCU_TOT_N01893))/lag12(WCU_TOT_ASIAGRP);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to clean this up by looping based on a client ID list? I know this can be done in a macro, just not sure if it can be done in a datastep.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for example (pseudo code):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;client_list="N01144 N01874 N00369"&lt;/P&gt;&lt;P&gt;loop&amp;nbsp;client_list&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WCU_CGASIA_&amp;amp;client_list=(WCU_TOT_&amp;amp;client_list-lag12(WCU_TOT_&amp;amp;client_list))/lag12(WCU_TOT_ASIAGRP);&lt;/P&gt;&lt;P&gt;endloop&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thx&lt;/P&gt;</description>
      <pubDate>Thu, 24 May 2018 13:03:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464730#M285121</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2018-05-24T13:03:35Z</dc:date>
    </item>
    <item>
      <title>Re: Looping in a Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464746#M285122</link>
      <description>&lt;P&gt;Try this with array processing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data EGTASK.ASIA_GRP_GC_WITHIN_TMP;
set EGTASK.ASIA_GRP_WCUTOT_TMP2;
array WCU_CGASIA{*}
  WCU_CGASIA_N01144
  WCU_CGASIA_N01874
  WCU_CGASIA_N00369
  WCU_CGASIA_N00274
  WCU_CGASIA_N01870
  WCU_CGASIA_N00346
  WCU_CGASIA_N01305
  WCU_CGASIA_N00459
  WCU_CGASIA_N01296
  WCU_CGASIA_N00462
  WCU_CGASIA_N02229
  WCU_CGASIA_N01893
;
array WCU_TOT {*}
  WCU_TOT_N01144
  WCU_TOT_N01874
  WCU_TOT_N00369
  WCU_TOT_N00274
  WCU_TOT_N01870
  WCU_TOT_N00346
  WCU_TOT_N01305
  WCU_TOT_N00459
  WCU_TOT_N01296
  WCU_TOT_N00462
  WCU_TOT_N02229
  WCU_TOT_N01893
;
l_WCU_TOT_ASIAGRP = lag12(WCU_TOT_ASIAGRP);
WCU_TOT_ASIAGRP_G = WCU_TOT_ASIAGRP / l_WCU_TOT_ASIAGRP - 1;
do i = 1 to dim(WCU_CGASIA);
  WCU_CGASIA{i} = (WCU_TOT{i} - lag12(WCU_TOT{i})) / l_WCU_TOT_ASIAGRP;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 May 2018 13:22:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464746#M285122</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-05-24T13:22:36Z</dc:date>
    </item>
    <item>
      <title>Re: Looping in a Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464769#M285123</link>
      <description>&lt;P&gt;Do you actually have a separate variables for each client???&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, you might want to reconsider the data structure as I would think you have to constantly&amp;nbsp; add or remove variables as clients change.&lt;/P&gt;
&lt;P&gt;It is very likely that BY Group processing based on the clients would work and not require so many variables or code volatility.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 May 2018 14:19:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464769#M285123</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-05-24T14:19:51Z</dc:date>
    </item>
    <item>
      <title>Re: Looping in a Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464893#M285124</link>
      <description>&lt;P&gt;I don't think SAS can do this "for each" type of processing. The array doesn't help that much.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I had been hoping to do:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data EGTASK.FAK;
	set EGTASK.AMERR_GRP_WCUTOT_TMP2;
	WCU_TOT_AMERRGRP_G=WCU_TOT_AMERRGRP/lag12(WCU_TOT_AMERRGRP)-1;

	do v = N45604,N01213,N22826;
		WCU_CGAMERR_(v) = (WCU_TOT_(v) - lag12(WCU_TOT_(v))) / lag12(WCU_TOT_AMERRGRP);
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;where the variable v is passed below to get the following from the loop:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;WCU_CGAMERR_N45604=(WCU_TOT_N45604-lag12(WCU_TOT_N45604))/lag12(WCU_TOT_AMERRGRP);&lt;/P&gt;&lt;P&gt;WCU_CGAMERR_N01213=(WCU_TOT_N01213-lag12(WCU_TOT_N01213))/lag12(WCU_TOT_AMERRGRP);&lt;/P&gt;&lt;P&gt;WCU_CGAMERR_N22826=(WCU_TOT_N22826-lag12(WCU_TOT_N22826))/lag12(WCU_TOT_AMERRGRP);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It seems such a loop structure is not possible, but would be helpful&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-Bill&lt;/P&gt;</description>
      <pubDate>Thu, 24 May 2018 20:02:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464893#M285124</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2018-05-24T20:02:53Z</dc:date>
    </item>
    <item>
      <title>Re: Looping in a Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464895#M285125</link>
      <description>&lt;P&gt;If you transpose to a long dataset, the code can be made completely data-driven.&lt;/P&gt;</description>
      <pubDate>Thu, 24 May 2018 20:15:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464895#M285125</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-05-24T20:15:17Z</dc:date>
    </item>
    <item>
      <title>Re: Looping in a Datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464955#M285126</link>
      <description>&lt;P&gt;A simple example for how to use transpose:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input month WCU_TOT_AMERRGRP WCU_TOT_N45604 WCU_TOT_N01213;
cards;
1 5 1 2
;
run;

proc transpose data=have out=int;
var WCU_TOT_N:;
by month WCU_TOT_AMERRGRP;
run;

data int2;
set int;
customer = substr(_name_,10);
drop _name_;
run;

/* in here, sort by customer and month, do your calculation with "by customer",
   and then sort back by month */

proc transpose data=int2 out=want (drop=_name_) prefix=WCU_TOT_N;
by month WCU_TOT_AMERRGRP;
var col1;
id customer;
run;

/* repeat the last transpose for every newly created variable,
   and merge the resulting tables
   (one transpose like this can only accept one prefix) */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as you can see, there is never a need to actually loop through the customer names from code, the steps will take care of that for you. By all means, strive for such a solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 May 2018 06:32:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-in-a-Datastep/m-p/464955#M285126</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-05-25T06:32:04Z</dc:date>
    </item>
  </channel>
</rss>

