Help using Base SAS procedures

Sequence Number

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

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
Respected Advisor
Posts: 4,651

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

View solution in original post


All Replies
Trusted Advisor
Posts: 2,113

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
Respected Advisor
Posts: 4,651

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,040

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;

Respected Advisor
Posts: 4,651

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
Super Contributor
Posts: 440

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;

Respected Advisor
Posts: 3,777

Re: Sequence Number

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

Respected Advisor
Posts: 4,651

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
Super Contributor
Posts: 440

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 = 1Smiley Wink 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

Respected Advisor
Posts: 4,651

Re: Sequence Number

Tal wrote:

SAS does not even know about "sequence+1" but it instead jumps...

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
Super Contributor
Posts: 440

Re: Sequence Number

i  see.That clarify my confusion.Thanks a lot Smiley Happy

Super Contributor
Posts: 282

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 345 views
  • 7 likes
  • 6 in conversation