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
- /
- Analytics
- /
- Stat Procs
- /
- Weighted Least Squares across a list of variables

Topic Options

- Subscribe to 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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-01-2016 08:09 PM - last edited on 04-01-2016 09:08 PM by Reeza

Dear all,

Thank you for checking my post. I need to estimate Rsquares using weighted least squares using individual regressions:

LGD = alpha + beta * v(1) + epsilon

...

LGD = alpha + beta * v(2000) + epsilon

I wrote the code but am not sure that the code does what I want. How can I extract one variable from &list (I have 2000 variables) and run WLS? Could you please help me if anyone knows how to do it.

Thank you in advance.

```
%let macro = ub.test;
%let list =
GDP
FE
;
proc reg data=ub.test outest=ub.est tableout;
model LGD=&list/selection=rsquare b;
run;
** run the original regression to get the residuals**;
proc reg data=ub.test;
model LGD=&list;
output out=ub.pred residual=residual;
run;
** compute the absolute and squared residuals**;
data ub.resid;
set ub.pred;
absresid=abs(residual);
sqresid=residual**2;
proc reg data=ub.resid;
**run a regression with the absolute residuals vs. X to get the estimated standard deviation **;
model absresid=&list;
output out=ub.s_weights predicted=s_hat;
**run a regression with the squared residuals vs. X to get the estimated standard deviation **;
model sqresid=&list;
output out=ub.v_weights predicted=v_hat;
run;
** compute the weights using the estiamted standard deviations**;
data ub.s_weights;
set ub.s_weights;
s_weight=1/(s_hat**2);
label s_weight = "weights using absolute residuals";
data ub.v_weights;
set ub.v_weights;
v_weight=1/v_hat;
label v_weight = "weights using squared residuals";
** do the weighted least squares using the weights from the estiamted standard deviations **;
proc reg data=ub.v_weights;
weight v_weight;
model LGD=&list;
run;
```

Accepted Solutions

Solution

04-04-2016
02:29 PM

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

04-04-2016 01:38 PM - edited 04-04-2016 01:40 PM

This line is incorrect - it should reference the array created, var_list.

`do i = 1 to dim(macro);`

So it should be:

`do i = 1 to dim(var_list);`

EDIT:

You made the correction, I'm not sure why you commented out the line and removed the keep statement. Those are important. Give it a try with those included.

Yes, I understand you want to run it for every variable. So using a BY group is much more efficient.

All Replies

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

04-01-2016 09:01 PM - edited 04-01-2016 09:07 PM

Are you set on using proc reg, or can you use proc robustreg?

Otherwise you're pretty much doing what's detailed on this page:

http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/

But since you're going to be doing it multiple times I would highly suggest looking at restricting your output and how you might capture the results you need into tables. You can look at using the ODS SELECT statement to keep only the output you need as well as ODS OUTPUT to capture the output you need to tables.

My best suggestion though would be to transpose your data to a long format. Then you can switch your code to using a BY groups method and you do one iteration. Your results will be much easier to navigate.

Here's a short post that walks through it:

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

04-01-2016 09:14 PM

If you transposed your data the only change required to your code would be the BY statement in your Proc Reg's.

To transpose you can use proc transpose or a data step. Assuming the same data from your previous question, this is an example:

```
data long;
set &merged_macro1;
array var_list(*) &list;
do i = 1 to dim(macro);
variable = vname(var_list(i));
Value = var_list(i);
output;
end;
keep date v1 variable value ;
run;
```

proc sort data=long;

by variable date;

run;

Then modify your regression, the first one is illustrated below:

```
proc reg data=long outest=ub.est tableout;
by variable;
model LGD=value/selection=rsquare b;
run;
```

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

04-01-2016 09:35 PM

Dear Reeza,

Thank you so much for helping me and sending me a preliminary code. I will try to test it and let you know if it works. Again, thank you soo much!

Yelena

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

04-04-2016 01:35 PM

Dear Reeza,

Thank you very much for your help. I tried to run your SAS code but the program gives me some errors:

data long2;

set ¯o;

array var_list(*) &list;

do i = 1 to dim(macro);

variable = vname(var_list(i));

Value = var_list(i);

output;

end;

keep date LGD variable value ;

run;proc sort data=long;by variable date;run;

ERROR: The DIM, LBOUND, and HBOUND functions require an array name for the first argument.

ERROR: Variable VARIABLE not found.

I tried to modify the code:

data long;

set ¯o;

array var_list(*) &list;

do i = 1 to dim(var_list);

var_list{i} = vname(var_list{i});

**Value{i} = var_list{i};

output;

end;

run;

and the output that looks like:

Date LGD GDP FE i

. 50 0.5 3.1 1

. . 0.5 3.1 2

. . . 3.1 3

. . . . 4

. 30 2.3 4.5 1

But I am not sure that this is what I need. Am I right? I think what I have to do is to find the way to run weighted least squares one-by-one from the list of variables. For instance, say I have LGD, GDP, FE etc., I want to run LGD = f(GDP), LGD = f(FE), LGD = f(etc.).

I am going to check another link that you sent me. Thank you very much for your help.

Yelena

Solution

04-04-2016
02:29 PM

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

04-04-2016 01:38 PM - edited 04-04-2016 01:40 PM

This line is incorrect - it should reference the array created, var_list.

`do i = 1 to dim(macro);`

So it should be:

`do i = 1 to dim(var_list);`

EDIT:

You made the correction, I'm not sure why you commented out the line and removed the keep statement. Those are important. Give it a try with those included.

Yes, I understand you want to run it for every variable. So using a BY group is much more efficient.

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

04-04-2016 01:49 PM

Dear Reeza,

I have fixed my errors:

**data** long2;

set ¯o;

array var_list(*) &list;

do i = **1** to dim(var_list);

variable = vname(var_list(i));

value = var_list(i);

output;

end;

keep date LGD variable value ;

**run**;**proc** **sort** data=long2;by variable date;**run**;

No more errors. It works fine. Thank you for your help!

I'll try to now run the second part of the code.

Yelena

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

04-04-2016 02:43 PM

Dear Reeza,

Both parts of the code works. Thank you very, very much for your help.

I hope you have a great day!

Yelena