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

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:

x1x2x3
111
11201
1201
120202
1111
111203
11211
11212

Here is what I wanted:

x1x2x3o1o2o3
111111
11201112
1201121
120202122
1111211
111203212
11211221
11212222

 

wanted to do it efficiently and avoid multiple sorting. Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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;

View solution in original post

3 REPLIES 3
Reeza
Super User

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.


 

Astounding
PROC Star

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;

yymissing
Calcite | Level 5

yes, that is what I needed. thanks

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
  • 3 replies
  • 1367 views
  • 0 likes
  • 3 in conversation