Solved
Not applicable
Posts: 1

# creating sum variables

I'm really new to SAS and would appreciate some help..

Accepted Solutions
Solution
‎04-19-2015 06:59 PM
PROC Star
Posts: 8,164

## Re: creating sum variables

Like Fareeza said, such stats are easily calculated using one of the SAS procs.

Your problem clearly looks like a homework assignment and, if the solution code is your own, your calculation formula is wrong. You would definitely have to capture and use N somewhere in your formula.

You can easily include some minor proc sql code to capture and store the desired numbers in macro variables. And you can print them in your log so that you follow your prof's preferred approach. e.g.:

proc sql;

select count(*),sum(x),sum(y),sum(x**2),sum(y**2)

into :n,:xsum,:ysum,:x2sum,:y2sum

from Question1

;

quit;

%put &n;

%put &xsum;

%put &ysum;

%put &x2sum;

%put &y2sum;

Conversely, Using the proc print approach, you could do something like the following to get the desired N:

proc print data=Question1 n='# of cases:' noobs;

sum x y x2 y2;/*get sums of variables from this table to use in your analysis-I like this way best since I can see the actual calculations*/

run;

All Replies
Posts: 1,147

## Re: creating sum variables

Hope this is what you are expecting

data XY;

input x y;

ord=1;

datalines;

18 23

13 20

18 18

15 16

10 14

12 11

8 10

4 7

7 6

3 4

;

run;

proc sort data=xy;

by ord;

run;

data want;

set xy;

by ord;

if first.ord then do;

sumx=x;

sumy=y;

end;

else do;

sumx+x;

sumy+y;

end;

run;

Thanks,

Jag

Thanks,
Jag
PROC Star
Posts: 8,164

## Re: creating sum variables

Given your example, what do you want the result to look like?

Solution
‎04-19-2015 06:59 PM
PROC Star
Posts: 8,164

## Re: creating sum variables

Like Fareeza said, such stats are easily calculated using one of the SAS procs.

Your problem clearly looks like a homework assignment and, if the solution code is your own, your calculation formula is wrong. You would definitely have to capture and use N somewhere in your formula.

You can easily include some minor proc sql code to capture and store the desired numbers in macro variables. And you can print them in your log so that you follow your prof's preferred approach. e.g.:

proc sql;

select count(*),sum(x),sum(y),sum(x**2),sum(y**2)

into :n,:xsum,:ysum,:x2sum,:y2sum

from Question1

;

quit;

%put &n;

%put &xsum;

%put &ysum;

%put &x2sum;

%put &y2sum;

Conversely, Using the proc print approach, you could do something like the following to get the desired N:

proc print data=Question1 n='# of cases:' noobs;

sum x y x2 y2;/*get sums of variables from this table to use in your analysis-I like this way best since I can see the actual calculations*/

run;

🔒 This topic is solved and locked.