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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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;

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Ksharp
Super User

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;
Ksharp
Super User

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;
fpb1
Obsidian | Level 7
This worked perfectly, thank you. The IML solution as well, but as I mentioned, my data are too large for it.
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



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
  • 5 replies
  • 869 views
  • 2 likes
  • 4 in conversation