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

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 527 views
  • 0 likes
  • 3 in conversation