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.
aaa | bbb | ccc | ddd | |
eee | 0 | 0 | 0 | 0 |
fff | 0 | 1 | 0 | 0 |
ggg | 0 | 1 | 0 | 0 |
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;
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;
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;
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;
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.