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

I want to use proc print or something to display a table like this below. I have been trying to use arrays, but maybe I'm on the wrong track.

 

 aaabbbcccddd
eee0000
fff0100
ggg0100

 

This is my code so far which doesnt work.

 

data want (keep=C1-C12);
	array fname {4} $ 3 ('aaa', 'bbb', 'ccc', 'ddd');
	array sname {3} $ 3 ('eee', 'fff', 'ggg');
	array C3SP {4,3} $ 1 C1-C12;

do i = 1 to 4;
		do j = 1 to 3;
			if sname[j] in ('fff', 'ggg') and fname[i] = 'bbb' then myname = '1';
			else myname= '0';
			C3SP[i,j] = myname;
			put C3SP[i,j]=;
			output;
		end;
	end;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

It's not how you approached the problem, but this should get you close:

 

data want;
   do rows = 'eee', 'fff', 'ggg';
      do cols = 'aaa', 'bbb', 'ccc', 'ddd';
         if cols='bbb' and rows in ('fff', 'ggg') then value=1;
         else value=0;
         output;
      end;
   end;
run;
proc tabulate data=want;
   var value;
   class rows cols;
   tables rows, cols*value=' '*sum=' ';
run;

View solution in original post

4 REPLIES 4
Astounding
PROC Star

It's not how you approached the problem, but this should get you close:

 

data want;
   do rows = 'eee', 'fff', 'ggg';
      do cols = 'aaa', 'bbb', 'ccc', 'ddd';
         if cols='bbb' and rows in ('fff', 'ggg') then value=1;
         else value=0;
         output;
      end;
   end;
run;
proc tabulate data=want;
   var value;
   class rows cols;
   tables rows, cols*value=' '*sum=' ';
run;
simmwa
Fluorite | Level 6
Thanks Astounding, but what if I want the 0,1 to be character not numeric? In fact you could change the '0'to '@'
simmwa
Fluorite | Level 6

Not completely sure why, but this mod seems to work. Thanks !!!

Proc Tabulate - Using a character variable in the VAR Statement 

 

 proc format library=work;
invalue mtype
   '@'  = 1
   '&'  = 2
   '%'  = 3
;
value mtype
   1 = '@'
   2 = '&'
   3 = '%'
;
run;

data wantdev3;
   informat myname mtype.;
   format   myname mtype.;
	do rows = 'aaa', 'bbb', 'ccc', 'ddd';
		do cols = 'eee', 'fff', 'ggg';
			if cols in ('fff', 'ggg') and rows = 'bbb' then myname = 1;
			else myname= 2;
			output;
		end;
	end;
run;
proc tabulate data=wantdev3 ;
	var myname;
	class rows cols;
	table rows, cols*myname=' '*sum=' '*f=mtype.; 
run;
andreas_lds
Jade | Level 19

You may have a wrong understanding what arrays are in sas: they exist primary to access variables of same type that need to be processed in the same way. The result you show looks like a report, not like a dataset at all. Another idea: have a look at proc iml, i have hardly used it, but it is the tool in sas to work with matrices.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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