BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sascode
Quartz | Level 8

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;


 

View solution in original post

4 REPLIES 4
ballardw
Super User

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
Quartz | Level 8
Thank you, this works fine,
i was expecting to see count column transposed but it solved anyway.
ballardw
Super User

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

Ksharp
Super User

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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1283 views
  • 1 like
  • 3 in conversation