0% found this document useful (0 votes)
20 views

Group Assignment 2

The document describes a SAS macro that creates a transition matrix from a dataset. The macro takes input data, and two variables to create frequency tables. It combines the tables, transforms the data, and outputs the final transition matrix. The macro is designed to process multiple years of data by iterating through years in a loop and running the main macro code for each year's data.

Uploaded by

bestreview7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Group Assignment 2

The document describes a SAS macro that creates a transition matrix from a dataset. The macro takes input data, and two variables to create frequency tables. It combines the tables, transforms the data, and outputs the final transition matrix. The macro is designed to process multiple years of data by iterating through years in a loop and running the main macro code for each year's data.

Uploaded by

bestreview7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Group Assignment 2: Transition Matrix

In this assignment, your task is to create a SAS code that can automatically calculate “Transition Matrix” within a given dataset “Transition Matrix”.

Here's an English description of each step in the SAS code:

**Macro `migration` Description:**

- This macro takes three input parameters: `indata`, `rank1`, and `rank2`.

**Step 1:**

- The `proc freq` procedure is used to calculate frequency tables from the `indata` dataset.

- The tables are based on the variables specified as `rank1` and `rank2`.

- Various options (`noprint`, `nocol`, `norow`, `nopercent`, `nocum`) are used to control the output appearance.

- The results are stored in a dataset called `matrix1`.

- The `where` clause filters the data where the 'Default' variable equals 0.

**Step 2:**

- Another `proc freq` procedure is executed, this time for cases where 'Default' equals 1, by using `rank1` only.

- The results are stored in a dataset called `matrix2`.

**Step 3:**

- In this step, the values in `rank2` in `matrix2` are set to 15 for all rows.

- This effectively transforms the values in `rank2` to a constant value for all rows in `matrix2`.

**Step 4:**

- The datasets `matrix1` and `matrix2` are combined into a single dataset called `matrix`. The 'Default' variable is dropped from this dataset.

**Step 5:**

- The `matrix` dataset is sorted by the variable specified in `rank1` using the `proc sort` procedure.
**Step 6:**

- The `proc transpose` procedure is used to pivot the data. It transposes the data so that 'Prediction' becomes the row identifier, 'Real' becomes the column
identifier, and 'count' becomes the cell values.

- The result is stored in a dataset called `temp`.

**Step 7:**

- A new dataset called `temp1` is created with variable names (numeric values) renamed to corresponding values like "I-2," "I-3," etc., using the `rename`
statement.

- The variable `RR` is created to store these values.

- The data from `temp` is processed, and for each value of `c1`, `RR` is set to the corresponding value based on conditional logic.

**Step 8:**

- The final result is printed using `proc print`, displaying the 'temp' dataset with the new variable names and their values.

**Macro `migrationbyyear` Description:**

- This macro takes three input parameters: `indata`, `rank1`, and `rank2`.

- It's designed to process data for multiple years, from 2010 to 2023.

**Step 9:**

- A loop is used to iterate through the years from 2010 to 2023.

- For each year, a new dataset named `df_&i.` is created by filtering the data from `indata` where the 'Year' variable matches the current year.

- The `migration` macro is then called to process each year's data.

**Step 10:**

- The `migrationbyyear` macro is invoked, starting the process for multiple years.
Requirements:

1. Your solution must be implemented using SAS code exclusively. You must use Macro on this assignment

2. You are not allowed to use external tools like Excel for any intermediate steps. This automation will ensure that if more data is provided in the future, your
code can be rerun seamlessly without the need for extensive modifications.

3. The assignment is due by midnight on Nov 6th.

4. Please copy your SAS code into txt file and upload to D2L.

Good luck with your assignment!

Your Solution may look like the tables below:

Ratings I-2 I-3 I-4 I-5 I-6 I-7 S-1 S-2 S-3 S-4 P-1 P-2 P-3 D Total
I-2 . 1 3 . . . 1 . . . . . . . 5
I-3 . 1 21 28 1 . . . . . . . . . 51
I-4 . 5 16 122 35 10 3 4 . . . . . . 195
I-5 . 7 8 149 294 60 16 3 . 1 . . . . 538
I-6 . 3 17 122 540 407 80 38 11 5 1 . . 1 1225
I-7 . 1 18 201 757 1064 414 132 54 26 12 3 1 1 2684
S-1 . 1 3 138 915 1491 985 511 189 58 21 20 2 7 4341
S-2 . . 1 10 178 647 720 622 362 223 42 34 10 4 2853
S-3 . . . . 17 31 88 164 192 128 37 34 6 4 701
S-4 . . . . 4 6 16 26 38 47 17 11 1 3 169
P-1 . . . 1 4 3 5 12 16 34 14 8 3 4 104
P-2 . . . . . 1 3 3 2 2 3 1 . . 15
P-3 . . . . . . . . . . . . . . 0
Total 0 19 87 771 2745 3720 2331 1515 864 524 147 111 23 24 12881

2010
Row Labels I-2 I-3 I-4 I-5 I-6 I-7 S-1 S-2 S-3 S-4 P-1 P-2 P-3 D Grand Total
I-2 . . . . . . . . . . . . . . 0
I-3 . . . . . . . . . . . . . . 0
I-4 . . . 1 1 1 . . . . . . . . 3
I-5 . 1 1 2 2 4 1 1 . . . . . . 12
I-6 . 1 1 10 10 13 2 4 . . . . . 1 42
I-7 . . 2 12 44 49 13 3 . . 3 . 1 . 127
S-1 . 1 . 4 32 55 40 24 9 1 1 . . . 167
S-2 . . . 1 5 22 25 20 14 3 2 . . . 92
S-3 . . . . 1 2 5 10 6 3 . 4 . 1 32
S-4 . . . . . 1 3 3 1 2 2 . . 2 14
P-1 . . . . . . . . 1 2 1 1 . 1 6
P-2 . . . . . . . . . . 1 . . . 1
P-3 . . . . . . . . . . . . . . 0
Grand Total 0 3 4 30 95 147 89 65 31 11 10 5 1 5 496

2023
Row Labels I-2 I-3 I-4 I-5 I-6 I-7 S-1 S-2 S-3 S-4 P-1 P-2 P-3 D Grand Total
I-2 . 1 . . . . 1 . . . . . . . 2
I-3 . . 14 17 1 . . . . . . . . . 32
I-4 . . 4 54 15 3 1 1 . . . . . . 78
I-5 . . . 51 113 14 1 1 . . . . . . 180
I-6 . . . 7 136 100 3 3 3 2 1 . . . 255
I-7 . . . . 21 130 42 6 8 4 1 . . . 212
S-1 . . . . . 44 65 17 7 5 . 1 . . 139
S-2 . . . . . 4 25 18 . 16 . 1 . . 64
S-3 . . . . . . 1 11 3 . 2 . . . 17
S-4 . . . . . . . 1 5 . 2 . . . 8
P-1 . . . . . . . . . 2 1 1 . . 4
P-2 . . . . . . . . . . . . . . 0
P-3 . . . . . . . . . . . . . . 0
Grand Total 0 1 18 129 286 295 139 58 26 29 7 3 0 0 991

You might also like