BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
themanoj20080
Fluorite | Level 6
Here is the example. I have a macro and proc iml. In the PROC IML, after the loop ends I want five different means but I am not getting that.
%macro hello(X);
use &dataset; read all var{&X} into A;
m=mean(A);
print m;
%mend hello;

proc iml;
do i = 1 to 5;
X=randnormal(10,0,1);
firstcol=X[,1];
create newdata var{firstcol};
append;
close;
%let dataset=newdata;
%hello(firstcol);
call delete(dataset);
end;
quit;
1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

The call 

X=randnormal(10,0,1);

creates a vector that has 10 obs and 1 var.

 

Also, every time you run the CREATE statement, it overwrites the previous data set.

 

If you want a data set that has 5 columns, then put the variables into a matrix that has 5 cols and write the matrix to a data set:

 

proc iml;
X=randnormal(10,{0 0 0 0 0},I(5));
varNames = {X1 X2 X3 X4 X5};
print X[colname=varNames];
create newdata from X[colname=varNames];
append from X;
close;
quit;

proc print;run;

View solution in original post

8 REPLIES 8
Reeza
Super User
Any particular reason you're using IML here? it seems like it would be drastically easier to just use PROC MEANS.
themanoj20080
Fluorite | Level 6

This was the example that I created to ask question here. What I have is more complicated. But the idea is same. What I want is in each loop when I call the macro I should be getting different result. 

Rick_SAS
SAS Super FREQ

The call 

X=randnormal(10,0,1);

creates a vector that has 10 obs and 1 var.

 

Also, every time you run the CREATE statement, it overwrites the previous data set.

 

If you want a data set that has 5 columns, then put the variables into a matrix that has 5 cols and write the matrix to a data set:

 

proc iml;
X=randnormal(10,{0 0 0 0 0},I(5));
varNames = {X1 X2 X3 X4 X5};
print X[colname=varNames];
create newdata from X[colname=varNames];
append from X;
close;
quit;

proc print;run;
themanoj20080
Fluorite | Level 6

I am still not able to get what I am looking for. I tried this code.

%macro hello(X);
use &dataset; read all var{&X} into A;
m=mean(A);
print m;
%mend hello;


proc iml;
z=j(5,1,0);
do i = 1 to 5;
X=randnormal(10,0,1);
varNames={X1};
print X[colname=varNames];
create newdata from X[colname=varNames];
append from X;
close;
%let dataset=newdata;
%hello(X1);
z[i]=m[1];
end;
quit;

Following is the error message:
NOTE: Module RANDNORMAL loaded from the storage SASHELP.IMLMLIB.
NOTE: Module ROWVEC loaded from the storage SASHELP.IMLMLIB.
NOTE: Closing WORK.NEWDATA
NOTE: The data set WORK.NEWDATA has 10 observations and 1 variables.
ERROR: The data set WORK.NEWDATA is in use, cannot be created.
 
statement : CREATE at line 76 column 2
NOTE: Exiting IML.


ballardw
Super User

@themanoj20080 wrote:

I am still not able to get what I am looking for. I tried this code.

 

Then perhaps it is time to show what some data similar to what you are actually starting with and what the result for that starting data should be. BOTH, begin and end.

Showing code that doesn't work does not get us a lot closer to an actual solution. Showing the desired solution for actual data allows better understanding of the actual issue.

Rick_SAS
SAS Super FREQ

Your second posting contains the same program as your first post. You did not incorporate my suggestion.

themanoj20080
Fluorite | Level 6

Actually I got it to work. I just had to put "call delete(my data)" at the end. Thank you for your suggestion.

themanoj20080
Fluorite | Level 6

Need to add "call delete(mydata)" at the end. And it works.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

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