DATA Step, Macro, Functions and more

Create grand total column with transposed data

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Create grand total column with transposed data

I have data that I have transposed into numerous columns. The data is structured like this:

 

name      1-2018   2-2018  3-2018

x                   1            2           3

y                   4            5           6

z                   7            8           9

 

I want to add a total column to the end of this table, but do not know the best way to do so.  The column count changes from month to month as new columns are added. 

I'm not sure which PROC is the best way to tackle this.

Being new to SAS, I may not be searching for my topic the best way; if there are similar posts, I apologize.


Accepted Solutions
Solution
‎03-05-2018 02:11 PM
Super User
Posts: 22,845

Re: Create grand total column with transposed data

Posted in reply to kbone1975

Make sure you have a naming convention, then you can use the short cut list to reference your variables. So if each of the columns that you need to add start with MONTH_, such as Month_201801, Month_201802, etc then you can do the following:

 

Total = sum(of month_: );

The other approach is to use a long format and then use PROC TABULATE to create the summaries as needed for reporting.

View solution in original post


All Replies
PROC Star
Posts: 1,315

Re: Create grand total column with transposed data

Posted in reply to kbone1975
data have;
input name  $     _12018   _22018  _32018;
datalines;
x                   1            2           3
y                   4            5           6
z                   7            8           9
;

data want;
set have;
total=sum(of _numeric_);
run;
Solution
‎03-05-2018 02:11 PM
Super User
Posts: 22,845

Re: Create grand total column with transposed data

Posted in reply to kbone1975

Make sure you have a naming convention, then you can use the short cut list to reference your variables. So if each of the columns that you need to add start with MONTH_, such as Month_201801, Month_201802, etc then you can do the following:

 

Total = sum(of month_: );

The other approach is to use a long format and then use PROC TABULATE to create the summaries as needed for reporting.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 106 views
  • 3 likes
  • 3 in conversation