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.
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.
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.
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.
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.