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

Hello !

As you can see on the picture below, we i have two variables that display for each observation the variation of a continous variable (not displayed here) with the previous one and the next one. I'd like to create several groups that contain observations with close differences between each other.

 

Groups.PNG

 

What I did is : If abs(Diff_prev) < 0.01 or abs(Diff_Next) < 0.01 then Groupe=cats(Variable,1). 

But now i'd like to change the value of the "1" and increment it by 1 each time a new group i created. Also I would like that at each new variable, the counter resets to 1. 

 

Thank you in advance for your help 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@Mathis1 wrote:

Hello !

As you can see on the picture below, we i have two variables that display for each observation the variation of a continous variable (not displayed here) with the previous one and the next one. I'd like to create several groups that contain observations with close differences between each other.

 

Groups.PNG

 

What I did is : If abs(Diff_prev) < 0.01 or abs(Diff_Next) < 0.01 then Groupe=cats(Variable,1). 

But now i'd like to change the value of the "1" and increment it by 1 each time a new group i created. Also I would like that at each new variable, the counter resets to 1. 


What do you mean by "counter"? What is the value of "1" that gets incremented? Neither of these questions have obvious answers from what you have told us.


Whatever the answers, we will need (a portion of) your actual data set, as SAS data step code

(and not as screen captures) in order to provide code that works. The data you show us must contain sufficient examples such taht the "counter" and the value of "1" that gets incremented are included in the data.

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

@Mathis1 wrote:

Hello !

As you can see on the picture below, we i have two variables that display for each observation the variation of a continous variable (not displayed here) with the previous one and the next one. I'd like to create several groups that contain observations with close differences between each other.

 

Groups.PNG

 

What I did is : If abs(Diff_prev) < 0.01 or abs(Diff_Next) < 0.01 then Groupe=cats(Variable,1). 

But now i'd like to change the value of the "1" and increment it by 1 each time a new group i created. Also I would like that at each new variable, the counter resets to 1. 


What do you mean by "counter"? What is the value of "1" that gets incremented? Neither of these questions have obvious answers from what you have told us.


Whatever the answers, we will need (a portion of) your actual data set, as SAS data step code

(and not as screen captures) in order to provide code that works. The data you show us must contain sufficient examples such taht the "counter" and the value of "1" that gets incremented are included in the data.

--
Paige Miller
Mathis1
Quartz | Level 8

Here is the code :

 

Data Table;
Input ID Y VAR $;
datalines;
1 -1762.05 A
2 -776.07 A
3 -670 A
4 -535 A
6 -531 A
7 -528 B
8 -294 B
9 -281 B
10 -227 B
;
run;

%MACRO COMPARE(table= , var= );
Data &table.;
retain id;
set &table.;
id=_N_;
run;

data &table._COMPARE;
set &table.;
by ID;
set &table. ( firstobs = 2 keep = &var. rename = (&var. = Next_Measure) )
&table. ( obs = 1 drop = _all_ );
Label Next_Measure=Next_Measure;
Prev_Measure = lag(&var.) ;
Diff_prev= (&var.-lag(&var.))/lag(&var.) ;
Diff_Next= (Next_Measure-&var.)/&var. ;
run;
%MEND;

 

%COMPARE(table=Table, var=Y);

 

data TABLE_COMPARE ;
Set TABLE_COMPARE;
If abs(Diff_prev) < 0.01 or abs(Diff_Next) < 0.01 then Group= 1;
run;

 

What I'm looking to get is :

 

Data TABLE_COMPARE_WANT;
Set TABLE_COMPARE;
Input Group_WANT;
datalines;
1
.
.
2
2
1
.
.
2
;
run;

 

So I have two group 1 and group 2 for VAR A and Group1 and group 2 for VAR B. 

 

Thank You for your answer 🙂

 

NB : I didn't accept your message as a solution, I don't know how come it's considered as solved 😕 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 530 views
  • 0 likes
  • 2 in conversation