Quartz | Level 8

## Creating different groups with close features

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.

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: Creating different groups with close features

@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.

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
2 REPLIES 2
Diamond | Level 26

## Re: Creating different groups with close features

@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.

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
Quartz | Level 8

## Re: Creating different groups with close features

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.