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

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_1group_2group_3group_4valueorder_seq

red

  red100100
reddark dark50100
reddarkshoeshoe25100
blue  blue8888
bluelight light3388
bluelightsockssocks2288
green  green9999
greendark dark7799
greendarkpantspants6599

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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

 

 

 

 


 

View solution in original post

2 REPLIES 2
Reeza
Super User
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

 

 

 

 


 

andreas_lds
Jade | Level 19

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 11390 views
  • 1 like
  • 3 in conversation