Rens
Rens

Reputation: 512

SAS: create time ID variable with program (instead of using the point-and-click system)

SAS offers a point-and-click system to create a time ID variable from a certain starting date using a particular frequency (e.g. weeks, quarters, years).

Since I need to do this proces repeatedly, I like to use a code as it makes things much easier. My data covers 1985-2005 and is divided into quarters (which gives 21 years * 4 quarters = 84 observations).

The date variable column should look like this (or give any other sas date which can be formated): Date:

1985/1

1985/2

1985/3

1985/4

1986/1

etc.

Does anyone knows how to write a code for this?

Thank you very much in advance!

Rens (a PhD student in sociology working on the music charts)

Upvotes: 0

Views: 154

Answers (2)

Shenglin Chen
Shenglin Chen

Reputation: 4554

Use intnx function.

data have;
do i=0 by 1;
   date=intnx('quarter',yyq(1985,1),i);
   if date>yyq(2005,4) then return;
   output;
end;
format date yyqs6.;
run;

Upvotes: 0

data _null_
data _null_

Reputation: 9109

You can use a data step and the YYQ function.

data quarters;
   do year = 1985 to 2005;
      do quarter = 1 to 4;
         date = yyq(year,quarter);
         output;
         end;
      end;
   format date yyq.;
   run;
proc print;
   run;

Upvotes: 2

Related Questions