Hi,
I have a dataset with variables:
"group_1" , "group_2", "group_3", "group_4", "value"
I want to create a new variable, "order_seq", which takes the first value from variable "value" and retain that value to the next "group 1"
example data:
group_1 | group_2 | group_3 | group_4 | value | order_seq |
red | red | 100 | 100 | ||
red | dark | dark | 50 | 100 | |
red | dark | shoe | shoe | 25 | 100 |
blue | blue | 88 | 88 | ||
blue | light | light | 33 | 88 | |
blue | light | socks | socks | 22 | 88 |
green | green | 99 | 99 | ||
green | dark | dark | 77 | 99 | |
green | dark | pants | pants | 65 | 99 |
data want;
set have;
by group1;
retain order_seq;
if first.group_1 then order_seq = value;
run;
@HitmonTran wrote:
Hi,
I have a dataset with variables:
"group_1" , "group_2", "group_3", "group_4", "value"
I want to create a new variable, "order_seq", which takes the first value from variable "value" and retain that value to the next "group 1"
example data:
group_1 group_2 group_3 group_4 value order_seq red
red 100 100 red dark dark 50 100 red dark shoe shoe 25 100 blue blue 88 88 blue light light 33 88 blue light socks socks 22 88 green green 99 99 green dark dark 77 99 green dark pants pants 65 99
data want;
set have;
by group1;
retain order_seq;
if first.group_1 then order_seq = value;
run;
@HitmonTran wrote:
Hi,
I have a dataset with variables:
"group_1" , "group_2", "group_3", "group_4", "value"
I want to create a new variable, "order_seq", which takes the first value from variable "value" and retain that value to the next "group 1"
example data:
group_1 group_2 group_3 group_4 value order_seq red
red 100 100 red dark dark 50 100 red dark shoe shoe 25 100 blue blue 88 88 blue light light 33 88 blue light socks socks 22 88 green green 99 99 green dark dark 77 99 green dark pants pants 65 99
The code provided by @Reeza works fine, if the data is sorted by group1. So you have to either sort it before the data step or add the option "notsorted" (without quotes) to the BY statement. To work as expected, the data needs to be grouped by group1.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.