Hi, I would like to create a group variable where the value changes (ie a new group value is created) when there is a gap in the sequence of numbers.
data have;
input var;
cards;
112
114
115
116
117
118
119
120
121
122
183
184
185
186
187
188
189
200
201
202
;
run;
Is there some mad reason you want that "group variable" to be character? It is much easier with a numeric value.
data want; set have; retain group 1; dvar = dif(var); if dvar > 1 then group+1; drop dvar; run;
Retain will keep the value of a variable across the data step boundary.
The Dif function returns the value of current value-previous value if used carefully.
Incrementing numbers with +1 is just plain easier than dealing with characters. What value would you want after Z? Plus if the data is "large" you may not make your character variable long enough to hold all the needed characters.
data want;
set have;
x=dif(var);
retain counter 1;
if x > 1 then
counter+1;
letter_group=byte(64+counter);
keep var letter_group;
run;
Agree with @ballardw about letters though.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.