Help using Base SAS procedures

Help: Try to sum up columns with dynamic count

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Help: Try to sum up columns with dynamic count

Hello,

I have a data set with a dynamic number of columns. This means the count of the columns is growing every trimester by 1.

Now I should be able to add a additional line which is calculating the total (sum).

Does anybody have an idea?

The set looks like the following:

X_Class12012/12012/22012/32012/4
15006003001000
2400550250850
3100200110620

Now I should generate a line X_Class1:9999 with the totals of each column in it.

In addition at the 4/1/13 an new column 2013/1 will be added and the total must also be generatet automatically in there.

thank you


Accepted Solutions
Solution
‎01-09-2013 09:07 AM
Super User
Posts: 5,516

Re: Help: Try to sum up columns with dynamic count

Posted in reply to meatmuffin

My earlier solution should give you exactly the numbers you asked for.  It won't say "Total" because that wasn't part of the original problem, but that's easy to accomplish by making three changes:

where X_Class1 ne 'Total';

(where=(X_Class1 ne 'Total'))

X_Class1='Total';

Notice that ne : has become ne.  If  you're getting output that is not what you want, post what changes you would like to see.

Good luck.

View solution in original post


All Replies
Respected Advisor
Posts: 3,799

Re: Help: Try to sum up columns with dynamic count

Posted in reply to meatmuffin

In your SAS data set name the dynamic colums with a common root.  QTR2012_1 QTR2012_2 etc.

Then you can use a "SAS Variable List" such as QTR: to refer to all the variables in a consise way.

TOTAL=sum(OF qtrSmiley Happy;  The keyword OF is important for use in the function.

http://support.sas.com/documentation/cdl/en/lrcon/65287/HTML/default/viewer.htm#p0wphcpsfgx6o7n1sjtq...

http://support.sas.com/documentation/cdl/en/lrcon/65287/HTML/default/viewer.htm#p0wphcpsfgx6o7n1sjtq...

Super User
Posts: 5,516

Re: Help: Try to sum up columns with dynamic count

Posted in reply to meatmuffin

You're leaving a lot to the imagination here.  I'll assume (as data _null_ did) that your numeric variable names all begin with QTR.  Also, I'll assume that X_Class1 is character with a length of at least 6.  Also, I'll assume your data set doesn't contain any additional variables that you haven't told us about.

Here's an approach:

proc means data=have noprint;

   var qtr:;

   where X_Class1 ne : '1:';

   output out=totals (keep=qtrSmiley Happy mean=;

run;

data want;

   set have (where=(X_Class1 ne : '1:')) end=done;

   output;

   if done;

   set totals;

   X_Class1=cats('1:', _n_);

   output;

run;

Good luck.

Occasional Contributor
Posts: 6

Re: Help: Try to sum up columns with dynamic count

Posted in reply to meatmuffin

Thanks for the answers, but it didn't give me the result i expected.

I think I was not precisely enough. I'm sorry for that.

I try it once again.

At the moment I'm having the table as shown above by my first posting.

After summarizing the data, i would like to get the following table:

XClass_12012/12012/22012/32012/42013/1
15006003001000500
2400550250850300
3100200110620200
Total1000135066024701000

After summarizing the trimester the red line with the totals should be generated.

Thanks for the help.

Super Contributor
Posts: 1,636

Re: Help: Try to sum up columns with dynamic count

Posted in reply to meatmuffin

data have;

input A $ B1 b2 b3 b4 b5;

data have;

input A $ B1 b2 b3 b4 b5;

cards;

1 500 600 300 1000 500

2 400 550 250 850 300

3 100 200 110 620 200

;

data want;

  set have end=last;

  output;

  t1+b1;

  t2+b2;

  t3+b3;

  t4+b4;

  t5+b5;

  if last then do;

  b1=t1; b2=t2; b3=t3;b4=t4;b5=t5;a='Total';

  output;end;

  drop t:;

  proc print noobs;run;

                            A         B1      b2      b3     b4      b5

                           1         500     600    300    1000     500

                           2         400     550    250     850     300

                           3         100     200    110     620     200

                           Total    1000    1350    660    2470    1000

Solution
‎01-09-2013 09:07 AM
Super User
Posts: 5,516

Re: Help: Try to sum up columns with dynamic count

Posted in reply to meatmuffin

My earlier solution should give you exactly the numbers you asked for.  It won't say "Total" because that wasn't part of the original problem, but that's easy to accomplish by making three changes:

where X_Class1 ne 'Total';

(where=(X_Class1 ne 'Total'))

X_Class1='Total';

Notice that ne : has become ne.  If  you're getting output that is not what you want, post what changes you would like to see.

Good luck.

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 957 views
  • 6 likes
  • 4 in conversation