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

Hi,

 

I have a dataset called test_1, I would like to add a column named 'Group' such that 'Group' will be numbers from 1, 2, 3, ...

 

test_1.PNG

 

 

The logic is: New column 'Group' is within each 'Class' and 'DY_TYP',

Start from row 1, 'Group' = 1, if the difference for 'Price' column greater than 4, ''Group' add 1. ( row  2 - row 1 > 4, then on row 2, 'Group' = 2); otherwise 'Group' keeps the same. Same logic happens to row 3 and row 4. 

Since the first 4 rows are in a group ('Class' = PC1 and 'DY_TYP'  = WD), It ends. 

In this example, we have 'Group' = 1, 2, 2, 3 for the first 4 rows. 

 

And row 5 is 'PC2' for 'Class' column, we will start again from 'Group' = 1, and do the same thing. 

 

How to make this happen in SAS?

 

Thanks! 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Assuming your data set is grouped by CLASS DY_TYP:

 

data want;

set have;

by class dy_typ notsorted;

increase = dif(price);

if first.dy_typ then group = 1;

else if increase > 4 then group + 1;

drop increase;

run;

View solution in original post

6 REPLIES 6
MarkWik
Quartz | Level 8

Post your sample as data step with an expected output sample and not as pics

Astounding
PROC Star

Assuming your data set is grouped by CLASS DY_TYP:

 

data want;

set have;

by class dy_typ notsorted;

increase = dif(price);

if first.dy_typ then group = 1;

else if increase > 4 then group + 1;

drop increase;

run;

Crubal
Quartz | Level 8

That works. Thanks! 

Reeza
Super User

https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/

This tutorial walks through how to add the numeration values using BY group processing.

Reeza
Super User

I assumed you're programming here and not using DataFlux or DI Studio (since you posted in the Data Management forum). I moved the post to the Base Programming forum, which is more appropriate. If you're using DI Studio the approach would be different.

Crubal
Quartz | Level 8

Thanks @Reeza I am not using DI Studio, but EG. Please move it. 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1926 views
  • 1 like
  • 4 in conversation