Have some dynamic data sets with different variables each run.
Looking very similar to the below.
Banana_ Cherry Ice Lemon Heath_Bar
ID Split Sundae Swirl Frost Crunch
XCDF1483 1 0 1 0 0
JKTR48392 0 1 0 0 1
OPRT76231 1 1 0 0 1
VXIQ87126 1 0 1 0 0
KTUE61824 0 1 0 1 1
This code appears to be the solution - for an array with variables that are all dates, each variable a different month.
data want;
set have;
_t=put(0,rb8.);
array t(*) months1-months6;/**group your months vars with a double dash*/
want = n(of t(*))-count(peekclong (addrlong(t[1]), 48),_t) ;
drop _t;
run;
My variables are not dates, and the name of the variables are quite different each time.
There would be maybe twenty variables at most.
Result for each ID would be:
XCDF1483 2
JKTR48392 2
OPRT76231 3
VXIQ87126 2
KTUE61824 3
How do I count - and store the total count of non-zero values/non-null values for each unique ID in the data set as a new variable? Not counting "0" or null.
Again, result is stored in the data set as new variable "Total", or something similar.
Didn't use any string examples, but there will be some strings.
Note: A variable is either formatted numeric (0 or 1, nothing else) or a char.
Character variables do not have numbers, and numerics do not have characters.
Longest string variable is 30 letters.
Thanx.
From your example it looks like you just want to sum the numeric variables in your dataset. So use the _NUMERIC_ variable list.
data want ;
set have;
total=sum(of _numeric_);
run;
If there are other numeric variables you want to exclude then you can remove those. For example if had one ID variable and two date variables that you wanted to exclude the code might look like this:
data want ;
set have;
total=sum(of _numeric_,-sum(id,date1,date2));
run;
Note that SAS has two types of variables. Floating point numbers and fixed length character strings. The FORMAT attached to a variable just controls how the values are displayed but does not change the TYPE of the variable.
Hello,
Is it this that you want?
data have;
LENGTH ID $ 30;
input ID $ Banana_Split Cherry_Sundae Ice_Swirl Lemon_Frost Heath_Bar_Crunch;
cards;
XCDF1483 1 0 1 0 0
JKTR48392 0 1 0 0 1
OPRT76231 1 1 0 0 1
VXIQ87126 1 0 1 0 0
KTUE61824 0 1 0 1 1
;
run;
data want;
set have;
array nume{*} _NUMERIC_;
total=sum(of nume(*));
run;
/* end of program */
Koen
From your example it looks like you just want to sum the numeric variables in your dataset. So use the _NUMERIC_ variable list.
data want ;
set have;
total=sum(of _numeric_);
run;
If there are other numeric variables you want to exclude then you can remove those. For example if had one ID variable and two date variables that you wanted to exclude the code might look like this:
data want ;
set have;
total=sum(of _numeric_,-sum(id,date1,date2));
run;
Note that SAS has two types of variables. Floating point numbers and fixed length character strings. The FORMAT attached to a variable just controls how the values are displayed but does not change the TYPE of the variable.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.