BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
robertrao
Quartz | Level 8


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

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

11 REPLIES 11
Doc_Duke
Rhodochrosite | Level 12

check out this SUGI proceeding

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

Doc Muhlbaier

Duke

PGStats
Opal | Level 21

The RETAIN statement is not required here. The sum statement

sequence + 1;

is equivalent to

retain sequence 0;

sequence = sum(sequence, 1);

PG

PG
robertrao
Quartz | Level 8

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;

PGStats
Opal | Level 21

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
Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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;

data_null__
Jade | Level 19

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

PGStats
Opal | Level 21

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
Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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

PGStats
Opal | Level 21

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
Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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

Amir
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 11 replies
  • 1973 views
  • 7 likes
  • 6 in conversation