BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
yelena
Fluorite | Level 6

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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.

 

View solution in original post

7 REPLIES 7
Reeza
Super User

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/

A macro program for repeating a procedure multiple times

 

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:

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-run-multiple-regres...

 

 

 

Reeza
Super User

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;

 

yelena
Fluorite | Level 6

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

yelena
Fluorite | Level 6

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 &macro;
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 &macro;
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

 

Reeza
Super User

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.

 

yelena
Fluorite | Level 6

Dear Reeza,

I have fixed my errors:

 

data long2;

set &macro;

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

 

 

 

 

yelena
Fluorite | Level 6

Dear Reeza,

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

I hope you have a great day!

Yelena

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 2303 views
  • 1 like
  • 2 in conversation