Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- counting values in a column and storing in a variable

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 02-11-2015 04:18 PM
(881 views)

If I have a dataset as such:

PatID Num_1 Num_2 Denom

A 0 1 1

B 1 1 1

C 1 1 1

...

And I need to sum up the number of 1's in Num_1, Num_2, and Denom and store them in a variable then further utilize them as such:

Meas1 = Num_1/Denom

Meas2 = Num_2/Denom

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Try the code, thats what I assumed.

5 REPLIES 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Do these variable ever take values other than 0, 1 or possibly missing?

If not then

denom=sum(num_1,num_2);

or

denom = sum ( num_1*(num_1=1), num_2*(num_2=1));

or less code though less obvious what it is doing:

denom=count(cats(num_1,num_2),'1');

but is easier to add more variables to

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

There are no missing values, only 1's or 0's. So for num_1 which could have 50,000 variables I want to count how many 1's are in the entire column. Then count how many 1's are in the num_2 column. They represent different things so should be stored as separate variables. The Denom actually has 1 for all the observations since it's a denominator.

Perhaps this example is clearer:

ID Num_1 Num_2 Denom

A 0 1 1

B 1 1 1

C 1 1 1

D 0 1 1

E 0 0 1

F 0 1 1

So here, Num_1 has two 1's in the column. Num_2 has five 1's in the column. Denom has six 1's.

For Meas1 representing Num_1, it would be 2/6. For Meas2 representing Num_2, it would be 5/6.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

I assume you mean over rows, not columns.

Proc means works well:

proc means data=have sum;

var meas1 meas2 denom;

output out=want sum=/autoname;

run;

Proc print data=want;

run;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Try the code, thats what I assumed.

**Available on demand!**

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

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.