BookmarkSubscribeRSS Feed
ZRick
Obsidian | Level 7

It looks like your saying echos my point.

Then why do you use count as a counter, we should use _n_, right? So, count is not used at all, code should look like this:

data tmp2 tmp3;

set tmp;

array acc(*) accrual_1 accrual_2 accrual_3 accrual_4;

do _n_=1 to dim(acc);

if acc(_n_) in (1,2,3,4,5,6) then count+1;

end;

/* count is the number of variables that have same condition */

if _n_=4 then output tmp2;

else output tmp3;

run;

Am I right?

Linlin
Lapis Lazuli | Level 10

You can't replace count with _n_ in the sample code. count is used to calculate how many variables meet your condition (in (1,2,3,4,5,6)). In my sample code, if all the four variables are in (1,2,3,4,5,6) then output to tmp2(count will be 4) , if one or more variables are not in (1,2,3,4,5,6) then output to tmp3 (count will be less then 4).

FriedEgg
SAS Employee

%macro multiVin(vars= ,incond= ,link=or ,output= ,elsout=);

  %if %str(&link)=%str(or) %then %let ocond=%str(>1);

   %else %let ocond=dim(acc&sysindex);

  array acc&sysindex

  • &vars;
  •   array cnt&sysindex[1] _temporary_;

      cnt&sysindex[1]=0;

      do _n_=1 to dim(acc&sysindex);

       if acc&sysindex[_n_] in (&incond) then cnt&sysindex[1]+1;

      end;

      if cnt&sysindex[1]=&ocond then output &output;

      %if &elsout>. %then %do;

       else output &elsout;

      %end;

    %mend;

    data foo;

    input accrual_1-accrual_4;

    cards;

    1 2 1 2

    12 4 12 4

    3 5 3 5

    9 9 9 9

    ;

    run;

    data bar;

    set foo;

    %multiVin(vars=accrual_1-accrual_4 ,incond=%str(1,2,3,4,5,6) ,link=and);

    run;

       accrual_1       accrual_2       accrual_3       accrual_4

               1               2               1               2

               3               5               3               5

    data bar;

    set foo;

    %multiVin(vars=accrual_1-accrual_4 ,incond=%str(1,2,3,4,5,6) ,link=or);

    run;

       accrual_1       accrual_2       accrual_3       accrual_4

               1               2               1               2

              12               4              12               4

               3               5               3               5

    hackathon24-white-horiz.png

    2025 SAS Hackathon: There is still time!

    Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

    Register Now

    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
    • 17 replies
    • 8813 views
    • 6 likes
    • 5 in conversation