BookmarkSubscribeRSS Feed
dataMart87
Quartz | Level 8

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;

rommel_0-1655244920097.png

 

2 REPLIES 2
ballardw
Super User

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.

Reeza
Super User
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. 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 964 views
  • 2 likes
  • 3 in conversation