Hi All,
I have a data of 2 columns: Name and value.
Name and value are unique pair.
I want to create combination of 2 name and value.
For example, the output should be like
a_name | a_value | b_name | b_value
v1 3 v2 0
v1 3 v2 1
v1 3 v3 30
v1 3 v3 35
v1 9 v2 0
v1 9 v2 1
.....
Thank you,
HHC
data have;
input name $ value;
datalines;
v1 3
v1 9
v1 2
v2 0
v2 1
v3 30
v3 35
;run;
This is an instructional case to show the benefits of defining two hash iterators for a single hash object.
It's a hash analog of "do i=1 to n; do j=1 to n:" where there are subsequent references to x{i} and x{j}:
data have;
input name $ value;
datalines;
v1 3
v1 9
v1 2
v2 0
v2 1
v3 30
v3 35
run;
data want (drop=_:);
if 0 then set have (rename=(name=name1 value=value1))
have (rename=(name=name2 value=value2)) ;
declare hash h (dataset:'have (rename=(name=name2 value=value2))',ordered:'a');
h.definekey(all:'Y');
h.definedata(all:'Y');
h.definedone();
declare hiter hi ('h');
declare hiter hj ('h');
do _rci=hi.first() by 0 until (hi.next()^=0);
name1=name2;
value1=value2;
do _rcj=hj.first() by 0 until (hj.next()^=0);
if name1^=name2 then output;
end;
end;
run;
data have;
input name $ value;
datalines;
v1 3
v1 9
v1 2
v2 0
v2 1
v3 30
v3 35
;run;
proc sql;
create table want as
select a.*, b.name as name2, b.value as value2
from have a cross join have b
where a.name<>b.name and a.value<>b.value;
quit;
This sql join method will create "duplicate" combination.
proc sql;
create table want as
select a.*, b.name as name2, b.value as value2
from have a cross join have b
where a.name<>b.name;
quit;
data dup; set want;
if name ='v1' and value=3 and name2='v2' and value2=0 then output;
if name ='v2' and value=0 and name2='v1' and value2=3 then output;
run;
V1 V1 combi is not needed, Reeza.
HHC
This is an instructional case to show the benefits of defining two hash iterators for a single hash object.
It's a hash analog of "do i=1 to n; do j=1 to n:" where there are subsequent references to x{i} and x{j}:
data have;
input name $ value;
datalines;
v1 3
v1 9
v1 2
v2 0
v2 1
v3 30
v3 35
run;
data want (drop=_:);
if 0 then set have (rename=(name=name1 value=value1))
have (rename=(name=name2 value=value2)) ;
declare hash h (dataset:'have (rename=(name=name2 value=value2))',ordered:'a');
h.definekey(all:'Y');
h.definedata(all:'Y');
h.definedone();
declare hiter hi ('h');
declare hiter hj ('h');
do _rci=hi.first() by 0 until (hi.next()^=0);
name1=name2;
value1=value2;
do _rcj=hj.first() by 0 until (hj.next()^=0);
if name1^=name2 then output;
end;
end;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.