Integer Sequences in SAS

In Stata, I regularly find myself using the integer sequence command ( seq() )  available as part of the egen functions.  When I use SAS, I’m dismayed at how difficult creating an integer sequence can be.  There’s a website by Richard DeVenezia which goes over  a variety of ways to create integer sequences in SAS (index-within-group.sas).

I’m going to demonstrate how this code relates to the Stata egen seq() command.

Example 1: Simple Integer Sequence

The SAS code:
data new; do order = 1   by 1set old; output; end; run;

The Stata code:
egen order=seq(), from(1) block(1)
or
egen order=seq()

Example 2: Simple Integer Sequence,re-numbered after 3 

The SAS code:
data
 new; do order = 1   by 1 until (order=3); set old;  outputendrun;

The Stata code:
egen order=seq(), from(1) block(1) to(3)
or
egen order=seq(), to(3)

Example 3: Integer Sequence within ID

The SAS code:
data new; do order = 1   by 1 until (last.id); setold;  by id; output; end; run;

 The Stata code:
bys id: egen order=seq(), from(1) block(1)
or
bys id: egen order=seq()

 

Example 4: Integer Sequence within ID and TIME Variable

The SAS code:
data new; do order = 1   by 1 until (last.time); set old; by id time; outputendrun;

 The Stata code:
bys id time: egen order=seq(), from(1) block(1)
or
bys id time: egen order=seq()

 

 

 

 

 

 

This entry was posted in Stata Code in SAS. Bookmark the permalink.

Leave a Reply