- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 12-29-2019 01:53 PM
(799 views)
hi experties
i struck at a point how to compare two different row values
for example
input is like below
A B
1 2
2 3
3 4
5 6
6 7
4 5
7 .
then after modification, I want to see
A B C D E F G
1 2 3 4 5 6 7
2 3 4 5 6 7
3 4 5 6 7
5 6 7
6 7
7 .
4 5 6 7
please help me in this situation how can i write code
i struck at a point how to compare two different row values
for example
input is like below
A B
1 2
2 3
3 4
5 6
6 7
4 5
7 .
then after modification, I want to see
A B C D E F G
1 2 3 4 5 6 7
2 3 4 5 6 7
3 4 5 6 7
5 6 7
6 7
7 .
4 5 6 7
please help me in this situation how can i write code
4 REPLIES 4
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi @sai_kumar Welcome to SAS communities. Can you plz explain at point you are stuck and also how you want to compare across rows?
I'd assume the output order to be the same as input order like
4 5 6 7
7 .
rather than
7 .
4 5 6 7
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
please ignore the output order
just i want compare the column2 value with the column1 values
if column2 value is available in column1 values then column3 have to be prepare with the column2 value of the column 1 value
like
A B
1 2
2 4
4 7
in the above example i want compare B value 2
in the column A
value 2 is available in the column A
so then i want create column3 like C
so ouput would be
A B C
1 2 4
after completion of all columnB values comparition
again i want compare columnC values with columnA
and create columnD
and soo on until end
just i want compare the column2 value with the column1 values
if column2 value is available in column1 values then column3 have to be prepare with the column2 value of the column 1 value
like
A B
1 2
2 4
4 7
in the above example i want compare B value 2
in the column A
value 2 is available in the column A
so then i want create column3 like C
so ouput would be
A B C
1 2 4
after completion of all columnB values comparition
again i want compare columnC values with columnA
and create columnD
and soo on until end
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi @sai_kumar Pardon me as I wrote the below code before I saw your response to my previous post. It seems to be the below logic should be close to your requirement
data have;
input A B;
cards;
1 2
2 3
3 4
5 6
6 7
4 5
7 .
;
data want;
set have;
array t(6);
if _n_=1 then do;
if 0 then set have(rename=(b=_b));
dcl hash H (dataset:'have(rename=(b=_b))') ;
h.definekey ("a") ;
h.definedata ("_b") ;
h.definedone () ;
end;
k=b;
do until(_b=.);
rc=h.find(key:k);
n=sum(n,1);
t(n)=_b;
k=_b;
end;
drop k _b rc n;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It is called SEARCHING TREE problem .
data have;
input _start $ _end $;
cards;
1 2
2 3
3 4
5 6
6 7
4 5
7 .
;
run;
data want(keep=path);
if _n_ eq 1 then do;
length path _path $ 800 ;
if 0 then set have;
declare hash ha(hashexp:20,dataset:'have(where=(_start is not missing and _end is not missing))',multidata:'y');
ha.definekey('_start');
ha.definedata('_end');
ha.definedone();
declare hash pa(ordered:'y');
declare hiter hi_path('pa');
pa.definekey('n');
pa.definedata('n','path');
pa.definedone();
end;
set have;
count=1;n=1;_n=1;
path=catx('|',_start,_end);
pa.add();
do while(hi_path.next()=0);
if n ne 1 then pa.remove(key:_n);_n=n;
_path=path;
_start=scan(path,-1,'|');
rc=ha.find(); if rc ne 0 then output;
do while(rc=0);
if not findw(path,strip(_end),'|') then do;
if length(path)+length(_end)+1 gt lengthc(path) then do;
putlog 'ERROR: The length of path and _path are set too short';
stop;
end;
count+1;n=count;
path=catx('|',path,_end);
pa.add();
path=_path;
end; else output; /* It is a circle.*/
rc=ha.find_next();
end;
end;
pa.clear();
run;