Year | Quarter | Quarter_id |
2000 | 1 | 3 |
2000 | 2 | 6 |
2000 | 3 | 9 |
2000 | 4 | 12 |
2001 | 1 | 15 |
2001 | 2 | 18 |
2001 | 3 | 21 |
2001 | 4 | 24 |
2002 | 1 | 27 |
2002 | 2 | 30 |
2002 | 3 | 33 |
2002 | 4 | 36 |
2004 | 1 | 39 |
2004 | 2 | 42 |
2004 | 3 | 45 |
2004 | 4 | 48 |
2005 | 1 | 51 |
2005 | 2 | 54 |
2005 | 3 | 57 |
2005 | 4 | 60 |
i have year and quarter column , and i wanted to create quarter_id column , Kindly help on this in writing the code.
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;
Assuming your data is already in sorted order, this is probably simplest:
data want;
set have;
quarter_id + 3;
run;
To create a key, that can be repoduced anywhere, anytime, use a hash:
md5(cats(Year,'|',Quarter)
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.