## grouping variable a

Solved
Super Contributor
Posts: 301

# grouping variable a

Hi everyone, how can I create the variable b from a.

a                b

----            ------

1                1

1                1

2                2

2                2

3                3

4                4

4                4

1                5

1                5

2                6

3                7

4                8

4                8

Thnaks,

V

Accepted Solutions
Solution
‎09-03-2012 10:41 AM
Super Contributor
Posts: 474

## Re: grouping variable a

Or use LAG function:

data want;

set have;

if lag1(A) ne A then B+1;

run;

More here:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/a000212547.htm

Cheers from Portugal.

Daniel Santos @ www.cgd.pt

All Replies
Super User
Posts: 8,082

## Re: grouping variable a

You can use the NOTSORTED option on the BY statement.

data want ;

set have;

by a notsorted;

b+first.a;

run;

Solution
‎09-03-2012 10:41 AM
Super Contributor
Posts: 474

## Re: grouping variable a

Or use LAG function:

data want;

set have;

if lag1(A) ne A then B+1;

run;

More here:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/a000212547.htm

Cheers from Portugal.

Daniel Santos @ www.cgd.pt

Valued Guide
Posts: 765

## Re: grouping variable a

hi ... or ...

data x;

set x;

b + (lag(a) ne a);

run;

Super Contributor
Posts: 474

## Re: grouping variable a

Yep, same thing, different coding. That's a good alternative.

:smileylove: SAS!

Cheers from Portugal.

Daniel Santos @ www.cgd.pt

Regular Contributor
Posts: 184

## Re: grouping variable a

Often DIF leads to a more concise solution, vis a vis LAG. Not always.

b ++ ^^ coalesce( dif( a ) , 1 ) ;

Super Contributor
Posts: 1,636

## Re: grouping variable a

Hi Howles,

What are "++ ^^" in your code?

Thanks!

Valued Guide
Posts: 765

## Re: grouping variable a

Hi ... I'll leave why use ++ to Howard (I've never understood why it's used).

As for ^^, it's a handy way to create 0/1 variables.  For example, what if you want to create a variable that has a value of 1 if certain text is found in a character string and 0 if not.  The FIND returns a location, not 1 or 0, but ...

data _null_;

text = 'abcde';

x1 = find(text,'c');

x2 = ^find(text,'c');

x3 = ^^find(text,'c');

put x1= x2= x3=;

run;

x1=3 x2=0 x3=1

X1 is 3, the location of 'c'

X2 is 0 since 3 is not 0 or missing

X3 is 1 since it's just not X2 (or X3 is 1 if text is found, otherwise 0)

That make sense?

Super Contributor
Posts: 1,636

## Re: grouping variable a

Hi Mike,

Thank you for your explanation! I replaced "++" with "+" but I didn't see any difference in the output.

data have;

input a;

cards;

1

1

2

2

3

4

4

1

1

2

3

4

4

;

data want;

set have;

b + ^^ coalesce( dif( a ) , 1 ) ;

proc print;run;

Posts: 1,337

## Re: grouping variable a

The ^^ is functional, but I believe the "++" is strictly a mnemonic device, to be visually symmetric to:

b +-   (expression)

Functionally

b ++ (expression)

is the same as

b + (expression)

Super Contributor
Posts: 1,636

## Re: grouping variable a

Thank you! That is what I thought.

Regular Contributor
Posts: 184

## Re: grouping variable a

The first + is part of the syntax for the assignment statement. The second is a bit of dorfmania and basically does nothing. If I were really striving for carpenterian job security I could have made it

++ --  ^^

```MikeZdeb wrote:

Hi ... I'll leave why use ++ to Howard (I've never understood why it's used).

As for ^^, it's a handy way to create 0/1 variables.  For example, what if you want to create a variable that has a value of 1 if certain text is found in a character string and 0 if not.  The FIND returns a location, not 1 or 0, but ...

data _null_;
text = 'abcde';
x1 = find(text,'c');
x2 = ^find(text,'c');
x3 = ^^find(text,'c');
put x1= x2= x3=;
run;

x1=3 x2=0 x3=1

X1 is 3, the location of 'c'
X2 is 0 since 3 is not 0 or missing
X3 is 1 since it's just not X2 (or X3 is 1 if text is found, otherwise 0)

That make sense?
```
Posts: 3,167

## Re: grouping variable a

If using DOW, then b is actually '_n_':

data want;

do until (last.a);

set have;

by a notsorted;

b=_n_;

output;

end;

run;

Haikuo

Valued Guide
Posts: 765

## Re: grouping variable a

hi ... nice, then you can also try ...

data want;

do b=_n_ by 0 until (last.a);

set have;

by a notsorted;

output;

end;

run;

Posts: 3,167

## Re: grouping variable a

I like the use of 'by 0', quite neat. Thanks, Mike.

🔒 This topic is solved and locked.

Discussion stats
• 14 replies
• 480 views
• 8 likes
• 8 in conversation