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_Class1 | 2012/1 | 2012/2 | 2012/3 | 2012/4 |
---|---|---|---|---|
1 | 500 | 600 | 300 | 1000 |
2 | 400 | 550 | 250 | 850 |
3 | 100 | 200 | 110 | 620 |
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
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.
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 qtr:); The keyword OF is important for use in the function.
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=qtr:) 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.
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_1 | 2012/1 | 2012/2 | 2012/3 | 2012/4 | 2013/1 |
1 | 500 | 600 | 300 | 1000 | 500 |
2 | 400 | 550 | 250 | 850 | 300 |
3 | 100 | 200 | 110 | 620 | 200 |
Total | 1000 | 1350 | 660 | 2470 | 1000 |
After summarizing the trimester the red line with the totals should be generated.
Thanks for the help.
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
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.