BookmarkSubscribeRSS Feed
r0720959
Calcite | Level 5

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;

5 REPLIES 5
FredrikE
Rhodochrosite | Level 12

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

r0720959
Calcite | Level 5

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 

 

 

FredrikE
Rhodochrosite | Level 12

I am not really into vectors, can this help?

https://stackoverflow.com/questions/15511853/sas-proc-iml-length-of-a-vector

 

//Fredrik

Rick_SAS
SAS Super FREQ

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;
 
r0720959
Calcite | Level 5
Thanks a lot, it is just what I needed to complete my code ! 🙂

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1915 views
  • 0 likes
  • 3 in conversation