BookmarkSubscribeRSS Feed
deleted_user
Not applicable
How to calculate Column wise sum in DATA STATEMENT?

ex: data set
data Columnwisesum;
input num1;
Datalines;
100
200
300
400
500
600
;
run;
5 REPLIES 5
Cynthia_sas
SAS Super FREQ
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
Bill
Quartz | Level 8
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;
Cynthia_sas
SAS Super FREQ
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]
deleted_user
Not applicable
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
deleted_user
Not applicable
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 946 views
  • 0 likes
  • 3 in conversation