Help using Base SAS procedures

Variable counting

Reply
Contributor
Posts: 37

Variable counting

[ Edited ]

I have some variables I need to count given some conditions are satisfied and my current code isn't working as intended. I have around 50 variables, so I'd hate to have to type the names for all of them, but here is what I need to happen: 

 

If the variable contains a value of 0 or it's missing, don't count it. Otherwise it gets counted. Here is my code that isn't working correctly:

 

DATA CaresEndor; SET CaresSums;
IF (cares_sf_1-cares_sf_7) ne 0 then PhysEndor = n(of cares_sf_1-cares_sf_7);
RUN;

Here is what's happening:

 

cares_sf_1       cares_sf_2          cares_sf_3             Count

       0                       2                          1                         3

       0                       0                          .                          0

       2                       3                          1                         3

       0                      0                           0                         0

       .                        4                          0                         2

 

Here's what I need it to do:

cares_sf_1       cares_sf_2          cares_sf_3             Count

       0                       2                          1                         2

       0                       0                          .                          0

       2                       3                          1                         3

       0                      0                           0                         0

       .                        4                          0                         1

Any help would be appreciated.

 

Thank you.

Super User
Posts: 23,752

Re: Variable counting

1. Create an array for your variables.

2. Loop over your array and count values that are not 0. 

 

Here's a tutorial on arrays and loops

https://stats.idre.ucla.edu/sas/seminars/sas-arrays/

 

If you need further help post back your code and any errors you're getting.

Super User
Posts: 13,563

Re: Variable counting

[ Edited ]

It helps to provide what the data to start with actually looks like as.

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

What your current code is doing:

(cares_sf_1-cares_sf_7) ne 0

is actually doing teh aritmetic, subtracting Cares_sf_7 from cares_sf_1 and if the result is not equal to 0 then reporting the n.

 

If there is not an operation such as the functions n, min, max that take a variable list like (cares_sf_1-cares_sf_7) then the subtraction is used.

Ask a Question
Discussion stats
  • 2 replies
  • 94 views
  • 0 likes
  • 3 in conversation