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

data ex1;
input num $;
cards;
1
2
3
run;

I have one dataset and how to know number of relations to each record and output.

For Your reference:(output should be like below)

new_num_relation

1,2,3
1,2
2,3
1,3;

How to do in arrays, How to do by using do loops

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
ata ex1;
input num $;
datalines;
1
2
3
;

proc sql noprint;
select nobs into :nobs
from dictionary.tables
where libname = "WORK" and memname = "EX1";
quit;

%let length = %eval((%sysfunc(ceil(%sysfunc(log10(&nobs.))))+1)*&nobs.);

data want;
array nums{&nobs.} $ _temporary_;
length result $&length.;
do i = 1 to &nobs.;
  set ex1;
  nums{i} = num;
end;
do i = 1 to &nobs - 1;
  do j = i + 1 to &nobs;
    result = nums{i};
    do k = j to &nobs;
      result = catx(",",result,nums{k});
      output;
    end;
  end;
end;
keep result;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User
ata ex1;
input num $;
datalines;
1
2
3
;

proc sql noprint;
select nobs into :nobs
from dictionary.tables
where libname = "WORK" and memname = "EX1";
quit;

%let length = %eval((%sysfunc(ceil(%sysfunc(log10(&nobs.))))+1)*&nobs.);

data want;
array nums{&nobs.} $ _temporary_;
length result $&length.;
do i = 1 to &nobs.;
  set ex1;
  nums{i} = num;
end;
do i = 1 to &nobs - 1;
  do j = i + 1 to &nobs;
    result = nums{i};
    do k = j to &nobs;
      result = catx(",",result,nums{k});
      output;
    end;
  end;
end;
keep result;
run;
Ksharp
Super User
data ex1;
input num $;
cards;
1
2
3
;
proc transpose data=ex1 out=have;
var num;
run;
%let dsid=%sysfunc(open(ex1));
%let nobs=%sysfunc(attrn(&dsid,nlobs));
%let dsid=%sysfunc(close(&dsid));
data want;
 set have;
array col{*} col:;
array x{&nobs};
length want $ 200;
k=-1;
do i=1 to 2**&nobs;
rc=graycode(k, of x[*]);
call missing(want);
if sum(of x{*})>1 then do;
  do j=1 to &nobs;
    if x{j}=1 then want=catx(',',want,col{j});
  end;
  output;
end;
end;
keep want;
run;
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
  • 2 replies
  • 799 views
  • 0 likes
  • 3 in conversation