Hello Community,
I am trying to give a rank to a product in the order they appear on my table. Here is my data Have
customer_Id | order_id | order |
a | 168 | TV |
a | 169 | TV |
a | 170 | TV |
a | 171 | Radio |
a | 172 | Radio |
a | 173 | Radio |
b | 174 | TV |
b | 175 | TV |
b | 176 | TV |
b | 177 | TV |
b | 178 | TV |
b | 179 | TV |
c | 180 | TV |
c | 181 | TV |
c | 182 | Radio |
c | 183 | Radio |
c | 184 | Radio |
And below is my data Want.
Id | order_id | order | Rank |
a | 168 | TV | 1 |
a | 169 | TV | 1 |
a | 170 | TV | 1 |
a | 171 | Radio | 2 |
a | 172 | Radio | 2 |
a | 173 | Radio | 2 |
b | 174 | TV | 1 |
b | 175 | TV | 1 |
b | 176 | TV | 1 |
b | 177 | TV | 1 |
b | 178 | TV | 1 |
b | 179 | TV | 1 |
c | 180 | TV | 1 |
c | 181 | TV | 1 |
c | 182 | Radio | 2 |
c | 183 | Radio | 2 |
c | 184 | Radio | 2 |
Thanks for helping.
Try this
data have;
input customer_Id $ order_id order $;
datalines;
a 168 TV
a 169 TV
a 170 TV
a 171 Radio
a 172 Radio
a 173 Radio
b 174 TV
b 175 TV
b 176 TV
b 177 TV
b 178 TV
b 179 TV
c 180 TV
c 181 TV
c 182 Radio
c 183 Radio
c 184 Radio
;
data want;
set have;
by customer_Id order notsorted;
if first.customer_Id then rank = 0;
if first.order then rank + 1;
run;
Try this
data have;
input customer_Id $ order_id order $;
datalines;
a 168 TV
a 169 TV
a 170 TV
a 171 Radio
a 172 Radio
a 173 Radio
b 174 TV
b 175 TV
b 176 TV
b 177 TV
b 178 TV
b 179 TV
c 180 TV
c 181 TV
c 182 Radio
c 183 Radio
c 184 Radio
;
data want;
set have;
by customer_Id order notsorted;
if first.customer_Id then rank = 0;
if first.order then rank + 1;
run;
Thank you this works perfectly.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.