BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Dingdang
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

6 REPLIES 6
art297
Opal | Level 21

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

Dingdang
Fluorite | Level 6

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

ballardw
Super User

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.

Dingdang
Fluorite | Level 6

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!!

Amir
PROC Star

Hi,

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

Regards,

Amir.

Tom
Super User Tom
Super User

You are not keeping the new summed variables.

keep &gIS &gSHOULD sumIS sumSHOULD;

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 1642 views
  • 3 likes
  • 5 in conversation