DATA Step, Macro, Functions and more

Creating a sequence

Reply
Contributor
Posts: 47

Creating a sequence

YearQuarterQuarter_id
200013
200026
200039
2000412
2001115
2001218
2001321
2001424
2002127
2002230
2002333
2002436
2004139
2004242
2004345
2004448
2005151
2005254
2005357
2005460

 i have year and quarter column , and i wanted to create quarter_id column , Kindly help on this in writing the code.

Frequent Contributor
Posts: 144

Re: Creating a sequence

Hi subtrat1,

If your data always have all quarters, the easiest way to do it is sorting your data and then using the order on that data. 

proc sort data=Have;
  by year Quarter;
run;

Then you could create a sequence and multiply by 3

data want;
  set have;
  by year quarter;
  retain count 0;
  if first.quarter then count=count+1;
  Quarter_ID=count*3;
  drop count;
run;

If you don't have all quarters, I guess that the easiest way to do it is comparing from your origen

data want;
  set have;
  retain first_Year first_quarter;
  if _N_=1 then do;
    first_YEAR=Year;
    first_quarter=quarter;
  end;
  Quarter_ID=(Year-first_Year)*12+Quarter*3;
run;

 

Super User
Posts: 5,500

Re: Creating a sequence

Assuming your data is already in sorted order, this is probably simplest:

 

data want;

set have;

quarter_id + 3;

run;

Super User
Posts: 5,426

Re: Creating a sequence

To create a key, that can be repoduced anywhere, anytime, use a hash:

md5(cats(Year,'|',Quarter)
Data never sleeps
Ask a Question
Discussion stats
  • 3 replies
  • 266 views
  • 2 likes
  • 4 in conversation