Hi there, I have three keys that I will use them join all data sources. I wanted to create an order for the three keys once they are sorted ascending. Here is what the data looks like:
x1 | x2 | x3 |
1 | 1 | 1 |
1 | 1 | 201 |
1 | 20 | 1 |
1 | 20 | 202 |
11 | 1 | 1 |
11 | 1 | 203 |
11 | 21 | 1 |
11 | 21 | 2 |
Here is what I wanted:
x1 | x2 | x3 | o1 | o2 | o3 |
1 | 1 | 1 | 1 | 1 | 1 |
1 | 1 | 201 | 1 | 1 | 2 |
1 | 20 | 1 | 1 | 2 | 1 |
1 | 20 | 202 | 1 | 2 | 2 |
11 | 1 | 1 | 2 | 1 | 1 |
11 | 1 | 203 | 2 | 1 | 2 |
11 | 21 | 1 | 2 | 2 | 1 |
11 | 21 | 2 | 2 | 2 | 2 |
wanted to do it efficiently and avoid multiple sorting. Thanks.
I can't test it right now, but I think this is where you are headed:
data want;
set have;
by x1 x2 x3;
if first.x1 then do;
o1 + 1;
o2 = 1;
o3 = 1;
end;
else if first.x2 then do;
o2 + 1;
o3 = 1;
end;
else if first.x3 then o3 + 1;
run;
What's the logic for the keys, O1, O2, O3?
@yymissing wrote:
Hi there, I have three keys that I will use them join all data sources. I wanted to create an order for the three keys once they are sorted ascending. Here is what the data looks like:
x1 x2 x3 1 1 1 1 1 201 1 20 1 1 20 202 11 1 1 11 1 203 11 21 1 11 21 2 Here is what I wanted:
x1 x2 x3 o1 o2 o3 1 1 1 1 1 1 1 1 201 1 1 2 1 20 1 1 2 1 1 20 202 1 2 2 11 1 1 2 1 1 11 1 203 2 1 2 11 21 1 2 2 1 11 21 2 2 2 2
wanted to do it efficiently and avoid multiple sorting. Thanks.
I can't test it right now, but I think this is where you are headed:
data want;
set have;
by x1 x2 x3;
if first.x1 then do;
o1 + 1;
o2 = 1;
o3 = 1;
end;
else if first.x2 then do;
o2 + 1;
o3 = 1;
end;
else if first.x3 then o3 + 1;
run;
yes, that is what I needed. thanks
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.