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

Hello,

 

I was trying to do gradient descent  in SAS. Below is my Code. I got "Matrix has not been set to a value" error and it said the Matrix Jtheta_new has not been set to a value. I searched a lot but still can't fix it. Any idea? Thank you.

 

options fullstimer;proc iml;
    reset deflib=sashelp;
    use class;
    read all var {weight height} into me;
    m = nrow(me);

    *scaling feature;
    s1 = max(me[,1])-min(me[,1]);
    s2 = max(me[,2])-min(me[,2]);
    mean_s1 = mean(me[,1]);
    mean_s2 = mean(me[,2]);
    me[,1] = (me[,1]-mean_s1)/s1;
    me[,2] = (me[,2]-mean_s2)/s2;
    *scaling feature;
      theta_0 = 0;
    theta_1 = 0;
    x0 = 1;
    ov = 10;
    alpha = 0.03;
    *print me;
    rec = 0;
   do
while (ov>0.000000001); theta_0Old = theta_0; theta_1Old = theta_1; *compute old residual and collect data to plot r*numOfIteration; rec = rec + 1; r2 = 0; do i=1 to m; residual_tt =(theta_0Old*x0 + theta_1Old*me[i,2]) - me[i,1]; r2 = r2+residual_tt*residual_tt; end; Jtheta = r2/2/m; xy = xy//(rec||Jtheta); *compute old residual and collect data to plot r*numOfIteration; res = 0; res_1 = 0; do i=1 to m; residual_0 =(theta_0Old*x0 + theta_1Old*me[i,2]) - me[i,1]; res = res + (residual_0*x0); res_1 = res_1 + (residual_0*me[i,2]); end; theta_0 = theta_0Old - alpha*res/m; theta_1 = theta_1Old - alpha*res_1/m; *update residual and decide whether it's convergence; r2 = 0; do i=1 to m; residual_tt =(theta_0*x0 + theta_1*me[i,2]) - me[i,1]; r2 = r2+residual_tt*residual_tt; end; Jtheta_new = r2/2/m; ov = abs(Jtheta_new - Jtheta); *update residual and decide whether it's convergence; end; print ov; call pgraf(xy,'*','x','y','mmmmm'); theta_0_last = theta_0*s1+mean_s1-mean_s2*s1*theta_1/s2; theta_1_last = theta_1*s1/s2; print theta_0_last theta_1_last; run; quit;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Welcome to the SAS Community 🙂

 

The code works fine on my site and gives a seemingly usable result?

 

Remember however, that you do not want the Run Statement at the bottom. The Quit statement is enough.

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Welcome to the SAS Community 🙂

 

The code works fine on my site and gives a seemingly usable result?

 

Remember however, that you do not want the Run Statement at the bottom. The Quit statement is enough.

twopunchman
Calcite | Level 5

Thank you so much! It seems I misspelled a variable name in SAS. LOL. Also thank you for your suggestion about the run statement.

PeterClemmensen
Tourmaline | Level 20

Anytime. Glad you found your answer 🙂

Rick_SAS
SAS Super FREQ

If I may offer two suggestions: 

1. Do not use RESET deflib=Sashelp;

    If you try to write a data set you will get an error message the says you do not have write permission for that library.

   Instead, keep RESET deflib=WORK (the default) and use the two-level data set name when you want to read data. For example,

   use sashelp.class;

 

2. The PGRAF routine produces ancient line-plot output. Use the newer ODS graphics, such as 

title 'mmmmm';
call series(xy[,1],xy[,2]) label={'x' 'y'} ;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 4 replies
  • 1939 views
  • 0 likes
  • 3 in conversation