DATA Step, Macro, Functions and more

sum function

Accepted Solution Solved
Reply
Contributor
Posts: 57
Accepted Solution

sum function

Hi,

I have a dataset and i've saved the variables of it into two macro variables &gIS and &gSHOULD, i want to add two new columns to this dataset, summing over all the variables in &gIS and &gSHOULD. I wrote the following code but nothing happened Smiley Sad the want i got is the same as have

DATA libl.want;

SET libl.have;

keep &gIS &gSHOULD;

sumIS=sum(of &gIS);     /*In the hope that sumIS is the sum of all the variables in &gIS for all the observations. */

sumSHOULD=sum(of &gSHOULD);

RUN;

Can someone help me correct my code? Thanks very much.

BR  Dingdang


Accepted Solutions
Solution
‎11-14-2013 12:12 PM
Super User
Posts: 11,343

Re: sum function

What does &GLS look like? Using sum( of &gls) the sum function expects to see a variable list such as X1-x5 or X: If &GLS is a list of variables with spaces then the syntax is incorrect. If &gls is a list of variable or values then they should be comma delimited.

View solution in original post


All Replies
PROC Star
Posts: 7,467

Re: sum function

Can you provide example have and want datasets, as well as the code you are using to create the macro variables?

Contributor
Posts: 57

Re: sum function

hi Arthur,

thanks for the quick response. Here is the code that i used for creating the two macro variables look like this:

proc sql;

select Feld

into :gIS separated by ' '

from informappe

where ...; /*here i omitted the conditions */

select Feld

into :gSHOULD separated by ' '

from informappe

where...;

quit;

the two macro variables have the same number of variables saved, like 40.

and then i have a big dataset have, which contains all the variables, including all variables from gIS and gSHOULD. I would like to create a new dataset want, with all the variables in gIS and gSHOULD from have, and then add two new variables sumIS and sumSHOULD to it, which contains just the sum of the variables in gIS and the sum of variables in gSHOULD.

for example var1-var40 are included in gIS and var41-var80 are included in gSHOULD. If I just select these variables from have, one observation looks like:

var1 var2 var3 var4....var40 var41 var42...var80

1     1       1     1        1        2      2           2

then the sumIS and sumSHOULD for the same observation is 40 and 80. and want looks like:

var1 var2 var3 var4....var40 var41 var42...var80 sumIS sumSHOULD

1     1      1      1        1       2       2        2       40       80

I hope i made myself clear this time.

BR  Dingdang

Solution
‎11-14-2013 12:12 PM
Super User
Posts: 11,343

Re: sum function

What does &GLS look like? Using sum( of &gls) the sum function expects to see a variable list such as X1-x5 or X: If &GLS is a list of variables with spaces then the syntax is incorrect. If &gls is a list of variable or values then they should be comma delimited.

Contributor
Posts: 57

Re: sum function

I read the use of sum function again and corrected the macro variables to comma delimited and then used sum(&gIS) and then it worked!! best thanks!!

Super Contributor
Posts: 282

Re: sum function

Hi,

Do you get any warning or error messages? Perhaps post your log too.

Regards,

Amir.

Super User
Super User
Posts: 7,039

Re: sum function

You are not keeping the new summed variables.

keep &gIS &gSHOULD sumIS sumSHOULD;

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 313 views
  • 3 likes
  • 5 in conversation