<?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: using a macro  to convert columns to running total in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484516#M71833</link>
    <description>&lt;P&gt;a simple array operation should do i think&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
infile datalines delimiter=',';
INPUT 
SALES_ID $ MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY;
DATALINES;
A1,45,61,60,14,74,35
A2,65,72,68,79,70,30
A3,24,17,16,69,29,18
A4,81,84,46,82,49,34
A5,81,38,67,40,88,11
A6,52,89,47,48,68,70
A7,62,10,41,73,49,42
;
RUN;

data want;
set have;
array t MONDAY--SATURDAY;
do _i_=2 to dim(t);
t(_i_)=t(_i_)+t(_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;
&lt;P&gt;Btw, I haven't looked into your code. I just saw the HAVE and WANT&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 06 Aug 2018 19:59:33 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-08-06T19:59:33Z</dc:date>
    <item>
      <title>using a macro  to convert columns to running total</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484511#M71832</link>
      <description>&lt;P&gt;I&amp;nbsp; have a weekly report I am running where I need to take an unknown number of columns that I need to convert to a running total.&lt;/P&gt;&lt;P&gt;Please try to keep in mind I created this very simple example, the data including the headers are much more complicated,&amp;nbsp; Also I am trying to automate this so I never would have to actually look at the the file to run my program. Variable names will never be the same from day to day&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 478px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22273i61EC7B38A0C5A6EB/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;Below is the code I have started with&lt;/P&gt;&lt;P&gt;please let me know what I am doing wrong, Macros are not my specialty so I am very open if I am going the wrong way about this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
infile datalines delimiter=',';
INPUT 
SALES_ID $ MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY;
DATALINES;
A1,45,61,60,14,74,35
A2,65,72,68,79,70,30
A3,24,17,16,69,29,18
A4,81,84,46,82,49,34
A5,81,38,67,40,88,11
A6,52,89,47,48,68,70
A7,62,10,41,73,49,42
;
RUN;

PROC CONTENTS DATA = have OUT = COL_STEP1;
RUN;
/*The varnum greater than one will always be know, however the total number of columns will can not be hard coded*/
PROC SQL;
	CREATE TABLE COLS AS
	SELECT DISTINCT
	NAME AS COL,
	VARNUM AS COL_NUM
FROM COL_STEP1
WHERE VARNUM &amp;gt; 1
ORDER BY VARNUM;
QUIT;


PROC SQL NOPRINT;
	SELECT DISTINCT MAX(COL_NUM)-2 INTO :N
	FROM COLS;
QUIT;

PROC SQL NOPRINT;
	SELECT DISTINCT col INTO :col1
	FROM COLS
	having col_num = min(col_num);
QUIT;
DATA IFILE (KEEP = COL);
	SET COLS;
	WHERE COL_NUM &amp;gt; 1;
	IF &amp;amp;N = 1 THEN STOP;
RUN;


%PUT NOTE: &amp;amp;N columns to aggregate &amp;amp;col1;
PROC SQL;
	CREATE TABLE ID_TABLE AS
	SELECT
	SALES_ID,
	&amp;amp;COL1,
	&amp;amp;COL1 AS TOTAL
FROM HAVE;
QUIT;

%MACRO TEST;

%DO X = 1 %TO &amp;amp;N;
DATA _NULL_;
		SET IFILE (FIRSTOBS=&amp;amp;X OBS=&amp;amp;X);
		CALL SYMPUT ('COL', STRIP(COL));
	RUN;
PROC SQL;
	CREATE TABLE ID_TABLE AS
	SELECT A.SALES_ID,
	B.&amp;amp;COL + A.TOTAL AS &amp;amp;COL,
	B.&amp;amp;COL + A.TOTAL AS TOTAL
FROM ID_TABLE AS A 
LEFT JOIN HAVE AS B
ON A.SALES_ID  = B.SALES_ID;
QUIT;
%END;
%MEND TEST;
%TEST;










&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 19:47:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484511#M71832</guid>
      <dc:creator>dane77221</dc:creator>
      <dc:date>2018-08-06T19:47:57Z</dc:date>
    </item>
    <item>
      <title>Re: using a macro  to convert columns to running total</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484516#M71833</link>
      <description>&lt;P&gt;a simple array operation should do i think&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
infile datalines delimiter=',';
INPUT 
SALES_ID $ MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY;
DATALINES;
A1,45,61,60,14,74,35
A2,65,72,68,79,70,30
A3,24,17,16,69,29,18
A4,81,84,46,82,49,34
A5,81,38,67,40,88,11
A6,52,89,47,48,68,70
A7,62,10,41,73,49,42
;
RUN;

data want;
set have;
array t MONDAY--SATURDAY;
do _i_=2 to dim(t);
t(_i_)=t(_i_)+t(_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;
&lt;P&gt;Btw, I haven't looked into your code. I just saw the HAVE and WANT&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 19:59:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484516#M71833</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-08-06T19:59:33Z</dc:date>
    </item>
    <item>
      <title>Re: using a macro  to convert columns to running total</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484518#M71834</link>
      <description>&lt;P&gt;You could reasonably use the macro part of your code to determine the variable names, which you say will change from day to day.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As far as the running totals across the rows, DO NOT USE MACROS for this. That would be completely unnecessary and unpleasant and difficult to program. Arrays work fine here to compute running totals, and should run faster than any macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So after you have determined the variable names from PROC CONTENTS and placed them into a macro variable which I will call &amp;amp;varnames, something like this ought to work&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    array x &amp;amp;varnames;
    do i=2 to dim(x);
        x(i)=x(i)+x(i-1);
    end;
    drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Aug 2018 20:02:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484518#M71834</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-08-06T20:02:23Z</dc:date>
    </item>
    <item>
      <title>Re: using a macro  to convert columns to running total</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484520#M71835</link>
      <description>&lt;P&gt;Once again, a typical example why putting data (weekdays) into structure (variable names) is a VERY BAD IDEA.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;De-transpose your data, and it's all a matter of simple processing.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 20:09:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484520#M71835</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-08-06T20:09:29Z</dc:date>
    </item>
    <item>
      <title>Re: using a macro  to convert columns to running total</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484521#M71836</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/224531"&gt;@dane77221&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I&amp;nbsp; have a weekly report I am running where I need to take an unknown number of columns that I need to convert to a running total.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is a spread sheet way of thinking and causes a vast number of the issues involved with bad macro or other code.&lt;/P&gt;
&lt;P&gt;If you had the data such that it looked like:&lt;/P&gt;
&lt;P&gt;id dayofweek (numeric 1 to 7) or DATE&amp;nbsp; and value&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code might look like:&lt;/P&gt;
&lt;PRE&gt;DATA HAVE;
infile datalines delimiter=',';
INPUT SALES_ID $ @;
do day= 1 to 6;
   input value @;
   output;
end;
input;
DATALINES;
A1,45,61,60,14,74,35
A2,65,72,68,79,70,30
A3,24,17,16,69,29,18
A4,81,84,46,82,49,34
A5,81,38,67,40,88,11
A6,52,89,47,48,68,70
A7,62,10,41,73,49,42
;
RUN;

Proc sort data=have;
  by sales_id day;

data want;
   set have;
   by sales_id;
   if first.sales_id then runningtot=value;
   else runningtot+value;
run;

proc report data=want;
   columns sales_id day,runningtot;
   define sales_id/group;
   define day/ across;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you wanted a report to show those values across in column form then that is relatively simple with either proc report or tabulate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do you expect use to "know" what the "unknown number of columns" might be?&lt;/P&gt;
&lt;P&gt;If this data is coming from an external file then take control and make sense of the data before going any further. Reading data properly will say many headaches later.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 20:09:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484521#M71836</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-08-06T20:09:59Z</dc:date>
    </item>
    <item>
      <title>Re: using a macro  to convert columns to running total</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484522#M71837</link>
      <description>&lt;P&gt;Thank you so much for all the help!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 20:14:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/using-a-macro-to-convert-columns-to-running-total/m-p/484522#M71837</guid>
      <dc:creator>dane77221</dc:creator>
      <dc:date>2018-08-06T20:14:53Z</dc:date>
    </item>
  </channel>
</rss>

