<?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: Result by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932903#M366957</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/438581"&gt;@sasuser_8&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I know the data is structured in a strange way but there is a reason behind it. Can you elaborate on the idea SQL-macro variables because I have to do the calculation for the 12 months... It could be useful.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Just because the data may arrive in that wide format does not mean you have to do analysis in that format. Doing analysis in the long format makes programming easier, as shown above by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; and others. The solution from Tom works &lt;FONT color="#FF0000"&gt;&lt;EM&gt;without modification&lt;/EM&gt; &lt;/FONT&gt;even if you have 100 INV_M variables. A word to the wise: work with long data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 18 Jun 2024 20:01:11 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2024-06-18T20:01:11Z</dc:date>
    <item>
      <title>Result by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932478#M366829</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have this table:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
INPUT CATEGORY TYPE$ INV_M1	INV_M2	INV_M3 ;
CARDS;

1 A 5 5 9 
1 B 10 2 2  
2 A 2 2 3 
2 B 6 6 5 
3 A 8 7 9 
3 B 4 9 8 
4 A 10 8 9 
4 B 5 2 1 
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For each CATEGORY and for each month (M1,M2,M3), I have to calculate the following result (TYPE B / TYPE A)&lt;/P&gt;
&lt;P&gt;The result should be this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RESULT.jpg" style="width: 393px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97458i3A19C564C190EC9E/image-size/large?v=v2&amp;amp;px=999" role="button" title="RESULT.jpg" alt="RESULT.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What function can I use ?&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2024 19:50:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932478#M366829</guid>
      <dc:creator>sasuser_8</dc:creator>
      <dc:date>2024-06-14T19:50:37Z</dc:date>
    </item>
    <item>
      <title>Re: Result by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932490#M366835</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way given that data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by category;
   array in (*) INV_M1	INV_M2	INV_M3;
   array l (*) LINV_M1	LINV_M2	LINV_M3;
   Array r (*) Result_M1 Result_M2 Result_M3;
   LINV_M1 = Lag(INV_M1);
   LINV_M2 = Lag(INV_M2);
   LINV_M3 = Lag(INV_M3);
   if last.category then do;
      do i=1 to dim(In);
         r[i]= in[i]/l[i];
      end;
      output;
   end;
  drop i Inv_: Linv: type;
run;&lt;/PRE&gt;
&lt;P&gt;However the data is poorly structured it would be better to be&lt;/P&gt;
&lt;P&gt;Category Month B A. and likely the Months should actually be a date instead of some random number like 1, 2 or 3 Or 25...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Code like this can reshape the data to that nicer form for calculating the result:&lt;/P&gt;
&lt;PRE&gt;proc transpose data=have out=trans (rename=(_name_=month));
   by category type;
   var Inv_m1 Inv_m2 Inv_m3;
run;

proc sort data=trans;
   by category month type;
run;

proc transpose data=trans out=need (drop=_name_);
   by category month;
   id type;
   var col1;
run;&lt;/PRE&gt;
&lt;P&gt;then&lt;/P&gt;
&lt;PRE&gt;data calc;
  set need;
  result= B / A;
run;&lt;/PRE&gt;
&lt;P&gt;Normally I would stop manipulating data and make a report for that set if you really need separate columns for the result such as one of these:&lt;/P&gt;
&lt;PRE&gt;proc report data=calc;
   columns category month,result;
   define category /group;
   define month/across "";
   define result/'';
run;
proc tabulate data=calc;
   class category month;
   var result;
   tables category='',
          month=''*result=''*sum=''
          /box='Category'
   ;
run;
&lt;/PRE&gt;
&lt;P&gt;if you really need a poorly structured data set:&lt;/P&gt;
&lt;PRE&gt;proc transpose data=calc out=want2 (drop=_name_)
    prefix=Result_;
  by category;
  id month;
  var result;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2024 20:44:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932490#M366835</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-14T20:44:05Z</dc:date>
    </item>
    <item>
      <title>Re: Result by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932495#M366836</link>
      <description>&lt;P&gt;Transpose the INV_Μ: variables to long layout by category and type.&lt;/P&gt;
&lt;P&gt;Then sort by category, _NAME_ and type, after which it is easy to compare the two successive values in a DATA step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=long;
by category type;
var inv_m:;
run;

proc sort data=long;
by category _name_ type;
run;

data want;
set long;
by category _name_;
l_col1= lag(col1);
if last._name_;
result = col1 / l_col1;
drop l_col1 col1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can transpose the want dataset to wide,but I recommend to keep the long layout, as it is much more flexible for further analysis.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2024 20:48:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932495#M366836</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-06-14T20:48:33Z</dc:date>
    </item>
    <item>
      <title>Re: Result by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932528#M366846</link>
      <description>&lt;P&gt;Adding:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; the data is poorly structured and would be much easier to handle with a better data structure, which he describes.&lt;/P&gt;</description>
      <pubDate>Sat, 15 Jun 2024 11:50:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932528#M366846</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-06-15T11:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: Result by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932549#M366854</link>
      <description>&lt;P&gt;To divide two numbers they need to be on the same observation.&lt;/P&gt;
&lt;P&gt;Simplest way is to transpose your dataset so that the TYPE values, A and B, are the variable names&amp;nbsp; then MONTH values are stored into a variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have name=month out=have_t;
  by category;
  id type;
  var inv_m1-inv_m3;
run;

data want;
 set have_t;
 result=b/a;
 format result 5.2 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can then easily create a REPORT that looks like your request.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc report;
  column category month,(a b result);
  define category / group;
  define month / across ' ';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1718473107440.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97489i486558D831EED530/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1718473107440.png" alt="Tom_0-1718473107440.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Jun 2024 17:38:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932549#M366854</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-15T17:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: Result by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932888#M366950</link>
      <description>&lt;P&gt;I know the data is structured in a strange way but there is a reason behind it. Can you elaborate on the idea SQL-macro variables because I have to do the calculation for the 12 months... It could be useful.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2024 18:18:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932888#M366950</guid>
      <dc:creator>sasuser_8</dc:creator>
      <dc:date>2024-06-18T18:18:37Z</dc:date>
    </item>
    <item>
      <title>Re: Result by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932903#M366957</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/438581"&gt;@sasuser_8&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I know the data is structured in a strange way but there is a reason behind it. Can you elaborate on the idea SQL-macro variables because I have to do the calculation for the 12 months... It could be useful.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Just because the data may arrive in that wide format does not mean you have to do analysis in that format. Doing analysis in the long format makes programming easier, as shown above by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; and others. The solution from Tom works &lt;FONT color="#FF0000"&gt;&lt;EM&gt;without modification&lt;/EM&gt; &lt;/FONT&gt;even if you have 100 INV_M variables. A word to the wise: work with long data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2024 20:01:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/932903#M366957</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-06-18T20:01:11Z</dc:date>
    </item>
    <item>
      <title>Re: Result by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/933010#M366989</link>
      <description>&lt;P&gt;I'm gonna go with the proc transpose solution... Thanks !&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2024 14:43:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Result-by-group/m-p/933010#M366989</guid>
      <dc:creator>sasuser_8</dc:creator>
      <dc:date>2024-06-19T14:43:04Z</dc:date>
    </item>
  </channel>
</rss>

