Help using Base SAS procedures

Data Set Qustions

Reply
N/A
Posts: 0

Data Set Qustions

How to calculate Column wise sum in DATA STATEMENT?

ex: data set
data Columnwisesum;
input num1;
Datalines;
100
200
300
400
500
600
;
run;
SAS Super FREQ
Posts: 8,743

Re: Data Set Qustions

Hi:
The DATA statement is what starts your DATA step program ... this is the program where you are reading values for NUM1 into a SAS dataset.

You can't calculate the SUM of the NUM1 variable in a DATA statement. However, within a DATA step program, you can create a new variable to hold the total of the sum of the NUM1 values on every observation.

However, I wonder why you want to calculate the total of NUM1 in a DATA step program. Do you need a REPORT that shows the summary of all the NUM1 values??? You could do this with PROC PRINT, without creating any new variables in the dataset. (Or you could use any number of other procedures.)

If you do need to calculate the sum of NUM1 in the DATA step program, then you would need to create a new variable with an assignment statement. Possibly you would use a RETAIN statement so that you could retain the value of your total-holding variable from observation to observation. Then you have to decide how you want to deal with the total at the end of the input file -- what do you envision seeing in the output dataset??? You don't have any other identifying variables (like a category variable or name, etc), so how would you distinguish the total of 2100 from the "regular" observation lines?? This is what leads me to wonder whether you really just want a report with a summary line versus calculating the total of NUM1 in a DATA step program.

cynthia
Super Contributor
Posts: 291

Re: Data Set Qustions

I recommend that you also do some research in the documentation (sum statement) so that you'll understand how this works.

data Columnwisesum;
input num1;
totl+num1;
Datalines;
100
200
300
400
500
600
;
run;
SAS Super FREQ
Posts: 8,743

Re: Data Set Qustions

And I wonder whether this is more what is desired as the possible outcome, which doesn't require using the SUM statement and the implied RETAIN.

cynthia
[pre]
proc print data=columnwisesum label;
title '1a) Use SUM statement with PROC PRINT -- do not need to calc sum in DATA step';
sum num1;
var num1;
label num1 = 'NUM1';
run;

[/pre]
N/A
Posts: 0

Re: Data Set Qustions

Hi Cynthia

Thank you for your solution,
this is asked me in the interview, i defend the answer as you explain me in the above, but the interviewer except from me as follows
Obs NUM1
1 100
2 200
3 300
4 400
5 500
6 600
====
2100
by using DATA Statement,
so that i posted this question to get the answer, is there any way to find
thanks
Patil MG
N/A
Posts: 0

Re: Data Set Qustions

Hi Cynthia,

I am giving BASE SAS certification, If you have information, could you please let me know what are changes happen in SAS 9.2, what i need to cover in SAS 9.2 before taking certification, if you have pattern and sample questions on BASE SAS please post me.

Thanks
Patil MG
Ask a Question
Discussion stats
  • 5 replies
  • 225 views
  • 0 likes
  • 3 in conversation