Hello, the purpose of this is data manipulation practice.
I'm trying to use an array to count up the X's in the val1, val2, val3 variables. I'm trying to count across.
data have;
infile datalines dsd dlm=",";
input subject $ val1 $ val2 $ val3 $;
datalines;
001, x, , ,
002, x, x, ,
003, x, , ,
004, , , ,
005, x, x, x
;
run;
results should be:
subject sum
001 1
002 2
003 1
004 0
005 3
@Hello_there wrote:
Thanks for the quick solution. I would like to use an array bc it would make it more flexible for when i re run the code for different variable amounts.
That would allow you to use the DIM() function.
array list var1-var3;
sum=dim(list)-cmiss(of list[*]);
No need for an array.
data want;
set have;
sum = 3 - cmiss(of var1-var3);
keep subject sum;
run;
@Hello_there wrote:
Thanks for the quick solution. I would like to use an array bc it would make it more flexible for when i re run the code for different variable amounts.
That would allow you to use the DIM() function.
array list var1-var3;
sum=dim(list)-cmiss(of list[*]);
@Hello_there wrote:
Thanks for the quick solution. I would like to use an array bc it would make it more flexible for when i re run the code for different variable amounts.
Provide a more realistic set of values then. And maybe variable names. Many of the functions that do some of this sort of stuff will work with a variable list, such as @Tom 's CMISS example.
There are functions that work well for single character values in a similar manner, Countc. But if you specific words then examples are needed.
The basic approach if you really need to examine each value is going to look like:
Counter=0; do i= 1 to dim(arrayname); counter = sum(counter,<condition involving arrayname[i]>); end;
What that condition may look like depends a whole lot on the values.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.