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
The RETAIN statement is not required here. The sum statement
sequence + 1;
is equivalent to
retain sequence 0;
sequence = sum(sequence, 1);
PG
check out this SUGI proceeding
http://support.sas.com/resources/papers/proceedings11/091-2011.pdf
Doc Muhlbaier
Duke
The RETAIN statement is not required here. The sum statement
sequence + 1;
is equivalent to
retain sequence 0;
sequence = sum(sequence, 1);
PG
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;
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
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;
Because the SUM statement implies RETAIN. Look at the online documentation.
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
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
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
i see.That clarify my confusion.Thanks a lot
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.
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!
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.