BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
karthick_gopal
Calcite | Level 5
proc iml;
	x = {1,1,1,2,2,2,3,3,3};
	y = {0.5,0.6,0.5,1,1.2,1,1.5,1.5,1.6};
Start reg(x,y);
	Start SSE(alpha) global(x,y);
		m = nrow(y);
		res = j(1,m,0);
		res = ((y - (alpha[1] + x*alpha[2:ncol(alpha)]))##2)`;
		return(res);
	finish SSE;
	alpha = {0.5 30};
	m1 = nrow(y);
	optn = m1||2;
	con = {0 0,
		   . 1};
	call NLPLM(rc,xres,"SSE",alpha,optn,con);
	print rc;
	print xres;
return(xres);
finish reg;
	xres = reg(x,y);
	m1 = nrow(y);	
	xd = (j(m1,1,1) || x);
	sol = solve(xd`*xd,xd`*y); /*checking*/
	print sol;
	
quit;


		

Hi,

 

I wanted to create a Non negative least squares inside proc iml as above. I want to wrap the nlplm call inside a modue becuase the values of x and y changes in my program (ie, i do reg on diferent partitions of orginal data). I get error for this program because x and y are not defined in the module SSE needed for nlplm. How do i go about this?

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

First, you need to realize that there is NO SUCH THING as a "local" or "nested" module. All modules are global in scope.

 

Second, you might want to review the rules for local and global symbols in a module.

 

Together, these rules imply that you can run the program like this:

proc iml;
Start SSE(alpha) global(g_x,g_y);
	m = nrow(g_y);
	res = j(1,m,0);
	res = ((g_y - (alpha[1] + g_x*alpha[2:ncol(alpha)]))##2)`;
	return(res);
finish SSE;

Start reg(x,y) global(g_x, g_y);
   g_x = x; g_y = y;  /* copy into global symbols */
	alpha = {0.5 30};
	m1 = nrow(y);
	optn = m1||2;
	con = {0 0,
		   . 1};
	call NLPLM(rc,xres,"SSE",alpha,optn,con);
	print rc;
	print xres;
return(xres);
finish reg;

x = {1,1,1,2,2,2,3,3,3};
y = {0.5,0.6,0.5,1,1.2,1,1.5,1.5,1.6};
xres = reg(x,y);
m1 = nrow(y);	
xd = (j(m1,1,1) || x);
sol = solve(xd`*xd,xd`*y); /*checking*/
print sol;
	

View solution in original post

1 REPLY 1
Rick_SAS
SAS Super FREQ

First, you need to realize that there is NO SUCH THING as a "local" or "nested" module. All modules are global in scope.

 

Second, you might want to review the rules for local and global symbols in a module.

 

Together, these rules imply that you can run the program like this:

proc iml;
Start SSE(alpha) global(g_x,g_y);
	m = nrow(g_y);
	res = j(1,m,0);
	res = ((g_y - (alpha[1] + g_x*alpha[2:ncol(alpha)]))##2)`;
	return(res);
finish SSE;

Start reg(x,y) global(g_x, g_y);
   g_x = x; g_y = y;  /* copy into global symbols */
	alpha = {0.5 30};
	m1 = nrow(y);
	optn = m1||2;
	con = {0 0,
		   . 1};
	call NLPLM(rc,xres,"SSE",alpha,optn,con);
	print rc;
	print xres;
return(xres);
finish reg;

x = {1,1,1,2,2,2,3,3,3};
y = {0.5,0.6,0.5,1,1.2,1,1.5,1.5,1.6};
xres = reg(x,y);
m1 = nrow(y);	
xd = (j(m1,1,1) || x);
sol = solve(xd`*xd,xd`*y); /*checking*/
print sol;
	

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 1 reply
  • 1194 views
  • 0 likes
  • 2 in conversation