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

I'm on SAS University Edition - I have a data set with 792 rows and 29 columns; each column is a variable with value "0" or "1".  I would like to make a table with 792 rows and a single column, with the value of the single column being the number of "1"s in the corresponding observation.

 

For example:

 

varA     varB     varC     varD

1          1           0          1

0          0           0          1

1          1           1          0

1          1           1          1

0          0           0          0

1          0           1          0

 

output:

3

1

3

4

0

2

 

Sorry if this is a basic question - I've new to SAS, and have looked around the forums and the internet without finding anything that has worked for me.  Much thanks!!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

I'm thinking that your real variable names don't all begin with "var".  If you want the sum of all numeric variables, you could use:

 

data want;

set have;

new_column = sum(of _numeric_);

run;

 

There's not much point in putting this new column into a separate data set.  Once you do that, if you were to ever sort your data you couldn't match the new column back to the row that it came from.

View solution in original post

4 REPLIES 4
art297
Opal | Level 21
data want (keep=sum);
  set have;
  array vars(*) varA--VarD;
  sum=sum(of vars(*));
run;

Art, CEO, AnalystFinder.com

 

kiranv_
Rhodochrosite | Level 12

this should work

data want(drop=var:);
 set have;
 finalvar=sum(of var:);
 run;
Astounding
PROC Star

I'm thinking that your real variable names don't all begin with "var".  If you want the sum of all numeric variables, you could use:

 

data want;

set have;

new_column = sum(of _numeric_);

run;

 

There's not much point in putting this new column into a separate data set.  Once you do that, if you were to ever sort your data you couldn't match the new column back to the row that it came from.

jsheu
Calcite | Level 5

Thanks, all! Looks like there a couple of ways to do this - I appreciate the help.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 3230 views
  • 1 like
  • 4 in conversation