devon
devon

Reputation: 321

Generate complete sequence giving min and max value in SAS?

So my data looks something like this

OBS _Seq_ 
1  15
2  16
3  20
4  22

I want a new data set that have a complete sequence of Seq. So something like this:

OBS _Seq_ 
1  15
2  16
3  17
4  18
5  19
6  20
7  21
8  22

I tried.

data _null_;
set data;
%let minSeq = min(_Seq_);
%let maxSeq = max(_Seq_);
run;

data newSeq;
do sequence = &minSeq to &maxSeq;
output;
end;
run;

Upvotes: 0

Views: 210

Answers (1)

DWal
DWal

Reputation: 2762

In order to assign a data step value to a macro variable, you need to use call symput. _n_=1 is true when the first row is read, and eof (initialized by the end= option in the set statement) signifies the last row of the data set. So assuming your data is sorted by _seq_:

data _null_;
  set data end=eof;
  if _n_=1 then call symput('minSeq',put(_Seq_,20.));
  if eof   then call symput('maxSeq',put(_Seq_,20.));
run;

Upvotes: 1

Related Questions