turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- programming in SAS for dynamically changing variab...

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

06-20-2017 12:16 PM

I have a co-variance matrix of variables like this: The values are mirror image across diagonal line. Therefore below diagonal or upper diagonal can be made null for convenience. This is variance-co-variance matrix of the coefficients of a linear regression.

```
Obs Intercept length diameter height weight_w weight_s
1 0.15510 -0.29969 -0.05904 -0.20594 0.07497 -0.00168
2 -0.29969 3.46991 -3.50836 -0.01703 -0.04841 -0.14048
3 -0.05904 -3.50836 5.08407 -0.82108 -0.13027 0.10732
4 -0.20594 -0.01703 -0.82108 4.89589 -0.29959 0.30447
5 0.07497 -0.04841 -0.13027 -0.29959 0.13787 -0.18763
6 -0.00168 -0.14048 0.10732 0.30447 -0.18763 0.40414
```

Where Obs 1 – 6 represent Intercept, and five variables length, diameter, height, weight_w, weight_s. Diagonal values are variances. Rest are co-variances between variables and variables and intercept.

I want to create a formula, where number of variables can change and user can input those as parameters. Based on number of variables formula should dynamically expand or contract and calculate result. The formula for five variables will be like this: C1,C2, ...C5 are constant that comes with five variables from outside. These are beta co-efficient of a linear regression. These will vary based on number of variables.

```
0.15+(C1)^2 * 3.46 + (C2)^2 * 5.08 + (C3)^2 * 4.89 + (C4)^2*0.13 + (C5)^2*0.40 -- Covers all variances
+2*C1*-0.29 + 2*C2*-0.05 + 2*C3*-0.20 + 2*C4*-0.07 + 2*C5*-0.001 -- covers co-variance of all variables with intercept
+2*C1*C2*-3.50 + 2*C1*C3*-0.01 + 2*C1*C4*-0.04 + 2*C1*C5*-0.14 --covers co-variance of “length” with other leftover (minus intercept) variables
+2*C2*C3*-0.82+ 2*C2*C4*-0.13 + 2*C2*C5*0.10 -- covers co-variance of “diameter” with leftover variables
+2*C3*C4*-0.29+ 2*C3*C5*0.30 -- covers co-variance of “height” with leftovers
+2*C4*C5*-0.18
```

Those five constants, matching to five variables, are inserted from outside. Rest are coming from co-variance matrix. In the co-variance matrix table the diagonal values are variances of those variables. Rest are co-variances. In the formula, you can see that where there are variances I have taken square of constants (Cs). Where there are co-variance, of two variables involved, the respective constants (Cs) multiple. Intercept is another term that comes with these variables. But intercept doesn't have any "C".

For two variables the co-variance matrix will be like this: Intercept will be there too.

```
Obs Intercept GRE GPA
1 1.15582 -.000281894 -0.28256
2 -0.00028 0.000001118 -0.00011
3 -0.28256 -.000114482 0.10213
```

Formula for calculation:

```
1.15582+(C1)^2 * 0.000001118 + (C2)^2 * 0.10213 -- Covers all variances on diagonal line
+2*C1*-.000281894 + 2*C2*-0.28256 -- covers co-variance of all variables with intercept
+2*C1*C2*-0.00011 -- covers co-variance between variable GRE & GPA
```

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sanojkumar

06-20-2017 12:53 PM - edited 06-20-2017 01:00 PM

This would be simple in PROC IML. It's really just matrix multiplication.

You can also do it in a SAS data step where you use macros to control the number of variables, but that's a more difficult.

--

Paige Miller

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to PaigeMiller

06-20-2017 02:30 PM

Unfortunately I don't have PROCIML. Any idea how to do using macros?

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sanojkumar

06-20-2017 03:04 PM - edited 06-20-2017 03:07 PM

As I said, it's probably not a simple piece of code, but if the user can input the variables of interest, then your SAS code would count the number of variables, store this count as a macro variable, and then write out the formula in a do loop, so that if there are 5 variables, the do loop uses all 5, and if there are 19 variables the do loop uses all 19.

Another thought: depending on exactly what you are doing, this may also be an application for ARRAYs in a DATA step. That may be the simplest way to do the math, but you'd still need a macro variable to contain the dynamically changing information provided by the user.

--

Paige Miller

Paige Miller