I have a list of row and column pairs. I would like to create a new dataset such that each row/column pair from the list is assigned a value of 1, otherwise a value of zero.
data start; input r c; datalines; 1 1 2 3 3 4 ; run;
I would like to end up with
1 0 0 0
0 0 1 0
0 0 0 1
0 0 0 0
I cannot use PROC IML because my actual dataset is too large and I do not have enough memory.
If you really want data step , try this one :
data start;
input r c;
datalines;
1 1
2 3
3 4
;
run;
%let nrow=4;
%let ncol=4;
data want;
if _n_=1 then do;
if 0 then set start;
declare hash h(dataset:'start');
h.definekey('r','c');
h.definedone();
end;
array x{&ncol.};
do r=1 to &nrow.;
do c=1 to &ncol.;
if h.check()=0 then x{c}=1;
else x{c}=0;
end;
output;
end;
keep x:;
run;
Can you back up and give us a broader picture of what this problem is? There are other ways of creating columns of zeros and ones that don't require PROC IML, if only we knew what you are doing we might be able to recommend some other way.
It is IML things. Hope you have SAS/IML .
data start;
input r c;
v=1;
datalines;
1 1
2 3
3 4
;
run;
proc iml;
use start;
read all var{v r c} into x;
close;
want=full(x);
create want from want;
append from want;
close;
quit;
proc print noobs;run;
If you really want data step , try this one :
data start;
input r c;
datalines;
1 1
2 3
3 4
;
run;
%let nrow=4;
%let ncol=4;
data want;
if _n_=1 then do;
if 0 then set start;
declare hash h(dataset:'start');
h.definekey('r','c');
h.definedone();
end;
array x{&ncol.};
do r=1 to &nrow.;
do c=1 to &ncol.;
if h.check()=0 then x{c}=1;
else x{c}=0;
end;
output;
end;
keep x:;
run;
Try this:
data start;
input r c;
datalines;
1 1
2 3
3 4
;
run;
proc sql noprint;
select max(max(r,c)) into :size
from start;
quit;
proc sort data = start;
by r c;
run;
data want;
set start end=eof;
by r c;
array x[&size.];
if first.r then
do _N_ = 1 to &size.;
x[_N_]=0;
end;
x[c] = 1;
if last.r then
do;
output;
r_count + 1;
end;
keep x:;
if eof and r_count NE &size. then
do;
do _N_ = 1 to &size.;
x[_N_]=0;
end;
do _N_ = r_count+1 to &size.;
output;
end;
end;
run;
proc print;
run;
Bart
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.