BookmarkSubscribeRSS Feed
lady8506
Quartz | Level 8

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.

2 REPLIES 2
Reeza
Super User

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.

ballardw
Super User

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.

sas-innovate-2024.png

Available on demand!

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

 

Register now!

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.

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
  • 2 replies
  • 709 views
  • 0 likes
  • 3 in conversation