Hello, I'd like to know how to use a vector as an input in a macro ?
My specific case is: I want the user to input a vector of events k={...} and a vector of total number of trials n={...}.
Then I'd like to use probbnml(p,n,k) function with each values of the vectors (like k(1) with n(1), k(2) with n(2) and so on...) Over a range of values for p (from 0.1 to 0.9 by 0.01 ).
I created a code to do it in SAS (9.4) but I'd like to convert it to a macro and I can't do it. Any help is welcome, thanks !
Here is the code I got:
proc iml;
p = t((0:99)/100);
n = {9,9,5};
k = {1,2,1};
z = j(300,3);
do i= 1 to 100;
do j=1 to 3;
a = (j-1)*100+i;
z[a,1] = p[i];
z[a,2] = probbnml(p[i],n[j],k[j]);
z[a,3] = j;
end;
end;
create data from z;
append from z;
close data;
quit;
proc sgplot data=data;
series x=col1 y =col2 / group= col3;
run;
Like this?
%macro test(_n=, _k=);
proc iml;
p = t((0:99)/100);
n = &_n;
k = &_k;
z = j(300,3);
do i= 1 to 100;
do j=1 to 3;
a = (j-1)*100+i;
z[a,1] = p[i];
z[a,2] = probbnml(p[i],n[j],k[j]);
z[a,3] = j;
end;
end;
create data from z;
append from z;
close data;
quit;
proc sgplot data=data;
series x=col1 y =col2 / group= col3;
run;
%mend;
%test(_n=%str({9,9,5}), _k=%str({1,2,1}));
//Fredrik
Yes almost !
Now I would just like for the second do loop to go from j=1 to j=length of the n vector.
And the z matrix to be of size (100*length of vector n,3).
Then it would be perfect
I am not really into vectors, can this help?
https://stackoverflow.com/questions/15511853/sas-proc-iml-length-of-a-vector
//Fredrik
I think FredrikE answered your macro question, but think your IML code will be more efficient if you eliminate the loop over i:
i = t(1:nrow(p));
do j=1 to nrow(n);
a = (j-1)*100+i;
z[a,1] = p;
z[a,2] = probbnml(p,n[j],k[j]);
z[a,3] = j;
end;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.