<?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: Array- PCT change from month to next month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950345#M371665</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Can you please show the full code?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; gives you the line of code to fix that will produce -0.11. From his post above&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if AAA[i] not in (.,0) then PCT_change[i]=(AAA[i+1]-AAA[i])/AAA[i];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PCT_change[i]=divide(AAA[i+1]-AAA[i],AAA[i]);&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;Also, Maxim 19.&lt;/P&gt;</description>
    <pubDate>Mon, 11 Nov 2024 12:26:53 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2024-11-11T12:26:53Z</dc:date>
    <item>
      <title>Array- PCT change from month to next month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950333#M371656</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;For each customer I have one row with 12 columns of values per month (month 1 is last month, month 2 is one month before last month, month 3 is two months before last month and so on).&lt;/P&gt;
&lt;P&gt;I want to calculate percentage change from one month to next month .&lt;/P&gt;
&lt;P&gt;So Need to calculate:&lt;/P&gt;
&lt;P&gt;PCT change from month 12 to month 11 (From value 90 to value&amp;nbsp; 80&amp;nbsp; so it should be -0.11 calculated by&amp;nbsp;(80-90)/90 )&lt;/P&gt;
&lt;P&gt;PCT change from month 11 to month 10&lt;/P&gt;
&lt;P&gt;PCT change from month 10 to month 9&lt;/P&gt;
&lt;P&gt;PCT change from month 9 to month 8&lt;/P&gt;
&lt;P&gt;PCT change from month 8&amp;nbsp; to month 7&lt;/P&gt;
&lt;P&gt;PCT change from month 7 to month 6&lt;/P&gt;
&lt;P&gt;PCT change from month 6 to month 5&lt;/P&gt;
&lt;P&gt;PCT change from month 5 to month 4&lt;/P&gt;
&lt;P&gt;PCT change from month 4 to month 3&lt;/P&gt;
&lt;P&gt;PCT change from month 3 to month 2&lt;/P&gt;
&lt;P&gt;PCT change from month 2 to month 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, My code doesn't calculate it well!!&lt;/P&gt;
&lt;P&gt;for example:&lt;/P&gt;
&lt;P&gt;PCT_change_12_To_11 should be&amp;nbsp; -0.11 but it shows&amp;nbsp;0.125&lt;/P&gt;
&lt;P&gt;What is wrong in my code???&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

Data have;
input Rev12 Rev11 Rev10 Rev9 Rev8 Rev7 Rev6 Rev5 Rev4 Rev3 Rev2 Rev1;
cards;
90 80 85 89 94 90 78 75 85 -9 24 32
;
Run;

data want(drop=i);
set have;
array AAA{*}
Rev12
Rev11 
Rev10
Rev9 
Rev8 
Rev7
Rev6 
Rev5 
Rev4
Rev3
Rev2
Rev1; 
array PCT_change{*} 
PCT_change_12_To_11
PCT_change_11_To_10
PCT_change_10_To_9 
PCT_change_9_To_8 
PCT_change_8_To_7 
PCT_change_7_To_6 
PCT_change_6_To_5 
PCT_change_5_To_4 
PCT_change_4_To_3 
PCT_change_3_To_2
PCT_change_2_To_1;

do i=1 to dim(AAA)-1; 
if AAA[i+1] not in  (.,0) then PCT_change[i]=(AAA[i]-AAA[i+1])/AAA[i+1];
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Nov 2024 08:23:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950333#M371656</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-11-11T08:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: Array- PCT change from month to next month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950336#M371658</link>
      <description>Should be :&lt;BR /&gt;if AAA[i+1] not in  (.,0) then PCT_change[i]=(AAA[i+1]-AAA[i])/AAA[i];&lt;BR /&gt;</description>
      <pubDate>Mon, 11 Nov 2024 08:39:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950336#M371658</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-11-11T08:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: Array- PCT change from month to next month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950337#M371659</link>
      <description>&lt;P&gt;but in mathematics condition is that&amp;nbsp; nominator cannot be 0 .&lt;/P&gt;
&lt;P&gt;In my code 1 is current month and 12 is the month one year ago.&lt;/P&gt;
&lt;P&gt;Please look again in my code:&lt;/P&gt;
&lt;P&gt;why calculation is wrong????&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
Data have;
input Rev12 Rev11 Rev10 Rev9 Rev8 Rev7 Rev6 Rev5 Rev4 Rev3 Rev2 Rev1;
cards;
0.96 0.96 0.97 0.84 0.83 0.87 0.87 0.77 0.80 0.97 0.94 0.94
;
Run;
data want(drop=i);
set have;
array AAA{*}
Rev1
Rev2 
Rev3
Rev4 
Rev5 
Rev6
Rev7 
Rev8 
Rev9
Rev10
Rev11
Rev12
 ; 
array PCT_change{*} 
PCT_change_1_2
PCT_change_2_3
PCT_change_3_4
PCT_change_4_5
PCT_change_5_6
PCT_change_6_7
PCT_change_7_8
PCT_change_8_9
PCT_change_9_10
PCT_change_10_11
PCT_change_11_12
;
do i=1 to dim(AAA)-1; 
if AAA[i+1] not in  (.,0) then PCT_change[i]=(AAA[i]-AAA[i+1])/AAA[i+1];
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Nov 2024 08:53:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950337#M371659</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-11-11T08:53:59Z</dc:date>
    </item>
    <item>
      <title>Re: Array- PCT change from month to next month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950338#M371660</link>
      <description>Then should be:&lt;BR /&gt;if AAA[i] not in (.,0) then PCT_change[i]=(AAA[i+1]-AAA[i])/AAA[i];&lt;BR /&gt;&lt;BR /&gt;You reverse role of AAA[i] and AAA[i+1],so you get wrong result.</description>
      <pubDate>Mon, 11 Nov 2024 09:09:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950338#M371660</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-11-11T09:09:35Z</dc:date>
    </item>
    <item>
      <title>Re: Array- PCT change from month to next month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950339#M371661</link>
      <description>&lt;P&gt;Can you please show the full code?&lt;/P&gt;
&lt;P&gt;Please remind that month 1 is last month and month 12 is the month one year ago&lt;/P&gt;
&lt;P&gt;(i is current and i+1 is previous)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Nov 2024 09:37:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950339#M371661</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-11-11T09:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: Array- PCT change from month to next month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950345#M371665</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Can you please show the full code?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; gives you the line of code to fix that will produce -0.11. From his post above&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if AAA[i] not in (.,0) then PCT_change[i]=(AAA[i+1]-AAA[i])/AAA[i];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also use&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PCT_change[i]=divide(AAA[i+1]-AAA[i],AAA[i]);&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;Also, Maxim 19.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Nov 2024 12:26:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950345#M371665</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-11-11T12:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: Array- PCT change from month to next month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950376#M371668</link>
      <description>&lt;P&gt;Not clear what you want to do.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first values are REV12=90 and REV11=80.&amp;nbsp; To get the percent change as - 1/9 you need to treat 90 as baseline and 80 as the new value.&amp;nbsp; So that means a change from month 12 to month 11.&amp;nbsp; That seems backwards.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is it that you measured?&amp;nbsp; What is it that you want to calculate?&lt;/P&gt;</description>
      <pubDate>Mon, 11 Nov 2024 18:24:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950376#M371668</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-11-11T18:24:17Z</dc:date>
    </item>
    <item>
      <title>Re: Array- PCT change from month to next month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950425#M371679</link>
      <description>&lt;P&gt;I think the problem comes from the fact that you define the arrays "backwards" in time, from 12 to 1 instead of from 1 to 12.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Take the first assignment in the array (I=1):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PCT_change[i]=(AAA[i]-AAA[i+1])/AAA[i+1];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;resolves to&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PCT_change[1]=(AAA[1]-AAA[2])/AAA[2];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which again resolves to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PCT_change_12_To_11=(rev12-rev11)/rev11;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But what you want is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PCT_change_12_To_11=(rev11-rev12)/rev12;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which should be written as&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PCT_change[i]=(AAA[i+1]-AAA[i])/AAA[i];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and you should also change the IF condition:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if AAA[i] not in  (.,0) then PCT_change[i]=(AAA[i+1]-AAA[i])/AAA[i];&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Nov 2024 07:59:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950425#M371679</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2024-11-12T07:59:00Z</dc:date>
    </item>
    <item>
      <title>Re: Array- PCT change from month to next month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950434#M371680</link>
      <description>&lt;P&gt;Maxim 19 at work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Rev12 Rev11 Rev10 Rev9 Rev8 Rev7 Rev6 Rev5 Rev4 Rev3 Rev2 Rev1;
cards;
90 80 85 89 94 90 78 75 85 -9 24 32
;

/* First, turn that stupid thing into usable data */

proc transpose data=have out=long1 (rename=(col1=value));
var rev:;
run;

data long2;
set long1;
month = input(substr(_name_,4),2.);
drop _name_;
run;

proc sort data=long2;
by month;
run;

/* Now the code is easy as it should be */
data want;
set long2;
PCT_change = ifn(_n_=1,.,(value - lag(value))/value);
format pct_change percent6.2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have multiple observations in the initial data, use some identifiying variable in BY statements and FIRST.[variable] as condition in the IFN.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2024 12:57:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950434#M371680</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-11-12T12:57:36Z</dc:date>
    </item>
    <item>
      <title>Re: Array- PCT change from month to next month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950437#M371681</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Maxim 19 at work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Now the code is easy as it should be */
data want;
set long2;
PCT_change = ifn(_n_=1,.,(value - lag(value))/value);
format pct_change percent6.2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have multiple observations in the initial data, use some identifiying variable in BY statements and FIRST.[variable] as condition in the IFN.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In addition to the above, you can now attach a format to variable MONTH (if you want) so that when you create output, instead of months 1 through 12, you can have month names such as JAN or JAN2023,&amp;nbsp;&lt;EM&gt;etc.&amp;nbsp;&lt;/EM&gt;This makes things easier for your audience, and looks more professional. Maxim 19 has many benefits!&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2024 14:00:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950437#M371681</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-11-12T14:00:17Z</dc:date>
    </item>
    <item>
      <title>Re: Array- PCT change from month to next month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950444#M371682</link>
      <description>&lt;P&gt;It seems you often look for ways to shorten your code.&amp;nbsp; This type of code is a good candidate:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if B not in (0, .) then C = A / B;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It can be shortened to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if B then C = A / B;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When SAS interprets logical expressions as true or false, 0 and missing values are false and all other values (including negative numbers) are true.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2024 14:53:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-PCT-change-from-month-to-next-month/m-p/950444#M371682</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2024-11-12T14:53:03Z</dc:date>
    </item>
  </channel>
</rss>

