## Sequence Number

Solved
Super Contributor
Posts: 1,041

# Sequence Number

Hi,

The following is the code for Sequence Number generation..

data sequence;

set final;

retain sequence;

by id;

if first.id then sequence=1;

else sequence+1;

run;

WHY is it necessary to say retain sequence????although i am using if first.id then sequence=1;

Could you please explain with a small example???

Thanks

Accepted Solutions
Solution
‎04-09-2013 01:45 PM
Posts: 5,543

## Re: Sequence Number

The RETAIN statement is not required here. The sum statement

sequence + 1;

is equivalent to

retain sequence 0;

sequence = sum(sequence, 1);

PG

PG

All Replies
Posts: 2,125

## Re: Sequence Number

check out this SUGI proceeding

http://support.sas.com/resources/papers/proceedings11/091-2011.pdf

Doc Muhlbaier

Duke

Solution
‎04-09-2013 01:45 PM
Posts: 5,543

## Re: Sequence Number

The RETAIN statement is not required here. The sum statement

sequence + 1;

is equivalent to

retain sequence 0;

sequence = sum(sequence, 1);

PG

PG
Super Contributor
Posts: 1,041

## Re: Sequence Number

So if I use

retain sequence 0;

sequence = sum(sequence, 1);

therer is no need of writing this if first.condition???

if first.id then sequence=1;

else sequence+1;

Posts: 5,543

## Re: Sequence Number

Your original code could be written

data sequence;

set final;

by id;

if first.id then sequence = 1;

else sequence + 1;

run;

or

data sequence;

set final;

retain sequence;

by id;

if first.id then sequence = 1;

else sequence = sum(sequence, 1);

run;

PG

PG
Super Contributor
Posts: 464

## Re: Sequence Number

so how come "sequence" does not get set to missing each time SAS gets to a new observation of "final"?

I mean it would have made sense it sequence was a part of the "final" set but in this case it  s not and still retains its  value.Is this kind of DOW?

data sequence;

set final;

by id;

if first.id then sequence = 1;

else sequence + 1;

run;

Posts: 3,852

## Re: Sequence Number

Because the SUM statement implies RETAIN.  Look at the online documentation.

Posts: 5,543

## Re: Sequence Number

As I stated before, the presence of the statement sequence + 1 causes sequence to be retained, just as if the statement retain sequence; was part of your program. DOW is the nickname of a technique that includes a DO WHILE(); or DO UNTIL(); loop including SET...; BY...; statements. It is not the case here.

PG

PG
Super Contributor
Posts: 464

## Re: Sequence Number

i know that "sequence+1" is the same as "retain sequence 0" and sum=sum(....). What i was wondering is how after checking the first statement (if id.first) and moving to the second observation of a by group "sequence" retains its value of 1? I thought newly created variables got set to missing every time SAS begins a new iteration within a by group.I mean after meeting the first condition (if first.id then sequence = 1 SAS does not even know about "sequence+1" but it instead jumps  to the second observation and still retains the value of sequence. So dont know what i am missing here

Posts: 5,543

## Re: Sequence Number

```Tal wrote:
```

Yes, SAS does know that sequence is retained because that can be determined during compilation (before execution begins). The same way it knows that sequence is numeric and not character. The simple fact that sequence appears in a sum statement (sequence + 1;) anywhere in the datastep, even if that statement never executes, is enough to give it the retained status.

hth

PG

PG
Super Contributor
Posts: 464

## Re: Sequence Number

i  see.That clarify my confusion.Thanks a lot

Super Contributor
Posts: 340

## Re: Sequence Number

Hi,

For sharing, just to see if it could be done without a retain statement and an if statement, I had some fun coming up with the following, although I wouldn't recommend it as a solution:

data sequence;

set final;

by id;

sequence+sum(first.id,first.id=0,-first.id*sequence);

run;

Regards,

Amir.

🔒 This topic is solved and locked.