Help using Base SAS procedures

How to numbering each observation

Reply
Occasional Contributor
Posts: 5

How to numbering each observation

Hi, everyone.

My question is that how to numbering each observation. (please look at the below dataset and focus on # of forecasts before the forecast column)

Company     Year   Quarter  forecast    # of forecasts before the forecast

      A            1993       1            xxx                                   0

      A                           1            xxx                                   1

      A                           2            xxx                                   2

      A                           3            xxx                                   3

      A                           4            xxx                                   4

      B             1993      1            yyy                                   0

      B                           2            yyy                                   1

      B                           3            yyy                                   2

      C             1995      1            zzz                                   0

      C                           2            zzz                                   1

      C                           3            zzz                                   2

Any comments or help will be appreciated. Thank you.

Best,

Steve

Super User
Posts: 10,023

Re: How to numbering each observation

data want;

set have;

by Company  ;

if first.Company   then n=-1;

n+1;

run;

Contributor
Posts: 27

Re: How to numbering each observation

data want;

set have;

by company;

if first.company then num=0;

else num+1;

run;

proc print;

run;

Occasional Contributor
Posts: 10

Re: How to numbering each observation

Note: code is not tested but i hope it fulfilled your requirement :

proc sort data=have;

by company;

run;

data req;

set have;

by company;

if first.company then num=1;

output;

num+1;

run;

Contributor
Posts: 23

Re: How to numbering each observation

You can try this one.

proc sort data=have;

by company;

run;

data want (drop=n);

set have;

by company;

if first.company then n=0;

n+1;

run;

Ask a Question
Discussion stats
  • 4 replies
  • 339 views
  • 2 likes
  • 5 in conversation