Hello, I have a dataset as it is given below. I need to create another table (single row) to have counts of 'f' for each variable. I am able to using proc sql but i am wondering how i can do that using arrays or proc iml which is new procedure in my sas knowledge.
Thank you.
data one;
input a $ b $ c $ d e;
cards;
a f a 1 3
f b f 2 4
a a a f 5
f f b 3 5
a a a f 6
a a a f 7
a a a 2 8
;
run;
One way (note change of variable D to character as written the "f" on some rows would be missing as your code has D as numeric.
data one; input a $ b $ c $ d $ e; cards; a f a 1 3 f b f 2 4 a a a f 5 f f b 3 5 a a a f 6 a a a f 7 a a a 2 8 ; data want; set one; array val(*) a b c d ; count = countc(cats(of val(*)),'f'); run;
Probably not the way you expected to see an array used though.
Perhaps you have an actual more complex problem in mind than counting a single character. If so you should be a bit more explicit. The Countc function counts a specific single character in this case, or use 'fF' to count upper and lower case f. The array is not actually needed though is a handy shortcut using the " of arrayname(*) " to process all the variables defined in the array especially with longer variable names. Countc (cats(a,b,c,d), 'f') would work without the array.
@sascode wrote:
Hello, I have a dataset as it is given below. I need to create another table (single row) to have counts of 'f' for each variable. I am able to using proc sql but i am wondering how i can do that using arrays or proc iml which is new procedure in my sas knowledge.
Thank you.
data one;
input a $ b $ c $ d e;
cards;
a f a 1 3
f b f 2 4
a a a f 5
f f b 3 5
a a a f 6
a a a f 7
a a a 2 8
;
run;
One way (note change of variable D to character as written the "f" on some rows would be missing as your code has D as numeric.
data one; input a $ b $ c $ d $ e; cards; a f a 1 3 f b f 2 4 a a a f 5 f f b 3 5 a a a f 6 a a a f 7 a a a 2 8 ; data want; set one; array val(*) a b c d ; count = countc(cats(of val(*)),'f'); run;
Probably not the way you expected to see an array used though.
Perhaps you have an actual more complex problem in mind than counting a single character. If so you should be a bit more explicit. The Countc function counts a specific single character in this case, or use 'fF' to count upper and lower case f. The array is not actually needed though is a handy shortcut using the " of arrayname(*) " to process all the variables defined in the array especially with longer variable names. Countc (cats(a,b,c,d), 'f') would work without the array.
@sascode wrote:
Hello, I have a dataset as it is given below. I need to create another table (single row) to have counts of 'f' for each variable. I am able to using proc sql but i am wondering how i can do that using arrays or proc iml which is new procedure in my sas knowledge.
Thank you.
data one;
input a $ b $ c $ d e;
cards;
a f a 1 3
f b f 2 4
a a a f 5
f f b 3 5
a a a f 6
a a a f 7
a a a 2 8
;
run;
@sascode wrote:
Thank you, this works fine,
i was expecting to see count column transposed but it solved anyway.
As I said, one way.
Another would be to examine each variable in the array and iterate a counter:
data want;
set one;
array val(*) a b c d ;
do i=1 to dim(val);
count = sum(count,countc(val[i],'f'));
end;
drop i;
run;
Depending on how you were thinking of transpose, transposed data might be one way to not use an array or variable list.
Here is IML code:
data one;
input a $ b $ c $ d $ e;
cards;
a f a 1 3
f b f 2 4
a a a f 5
f f b 3 5
a a a f 6
a a a f 7
a a a 2 8
;
proc iml;
use one;
read all var _char_ into x;
close;
count=(x='f')[,+];
create count var {count};
append;
close;
quit;
data want2;
merge one count;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.