# Automatically sorting years and quarters in a serial number

6 views (last 30 days)
Wesso on 28 Jan 2023
Answered: Caitlin O'Brien on 28 Jan 2023
Hi,
I have alarge sample of quarterly data. I have years (example 1990....2022) and quarters (1,2,3,4). How can i generate a serial number that sorts the years and numbers where all firms with year 1990 and quarter 1 will have a value of 1 ; then 1990 quarter 2 will have a value of 2 ....1991,quarter 1 will have a value of 5 etc....

Caitlin O'Brien on 28 Jan 2023
If you know the year you want to start with, I think this could work-
years = [1990 1991 1990 1992 1993 1999 1996];
quarters = [3 1 1 2 1 2 4];
startYr = min(years);
serialNo = 4*(years-startYr)+quarters
serialNo = 1×7
3 5 1 10 13 38 28
[srt ind] = sort(serialNo);
data = [srt' years(ind)' quarters(ind)']
data = 7×3
1 1990 1 3 1990 3 5 1991 1 10 1992 2 13 1993 1 28 1996 4 38 1999 2

Sulaymon Eshkabilov on 28 Jan 2023
One of the easy solutions is to cretae a table array with variable names: Year (numerical) and Quarter (categorical array), e.g. categorical array, e.g. Q1, Q2, Q3, Q4 per year and then by the Q names, you can sort your data.

### Categories

Find more on Descriptive Statistics in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by