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
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;
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;
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.