Array Function

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

Array Function

I have created dataset like

SubjidstudyidABCEF
R281017100100110
R281017100101010
R281017100210011
R281017100200100
R281017100300001

I want to check the variable A -F and print the result if it is More than 1 i want ouput in Variable Race as Done Else Output the Corresonding Variable Using Array Statement

I want the Output as

SubjidstudyidABCEFRace
R281017100100110Multiple
R281017100101010Multiple
R281017100210011Multiple
R281017100200100C
R281017100300001F

Could any one Help M


Accepted Solutions
Solution
‎03-23-2013 12:20 PM
Super User
Posts: 5,496

Re: Array Function

Posted in reply to RichardinOz

I like the idea, but consider that this might be a little simpler way to overwrite Race:

do i = 1 to dim(list);

  if list{i} > 0 then do;

     if Race= ' ' then Race = vname(list{i});

     else Race = 'Multiple';

  end;

end;

drop i;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,137

Re: Array Function

Posted in reply to SushilKumar

hi Sushil,

Please try the below codes it will give you the desired result. i used vname() function to get the variable name in the race column.

data have;

    input subjid $ studyid a b c d e;

cards;

R281017 1001 0 0 1 1 0

R281017 1001 0 1 0 1 0

R281017 1002 1 0 0 1 1

R281017 1002 0 0 1 0 0

R281017 1003 0 0 0 0 1

;

run;

data want;

    set have;

    array list(*) a b c d e;

    sums=sum(of list(*));

    if sums > 1 then race='Multiple';

    else race= vname(list[whichn(max(of list

  • ), of list
  • )]);
  •     drop sums;

    run;

    Let me know if this helped you.

    Thanks,

    Jagadish

    Thanks,
    Jag
    Contributor
    Posts: 32

    Re: Array Function

    Posted in reply to Jagadishkatam

    Hi Jagadishkatam,

    When I Use this Code its Showing Error , I Using Sas Version 8

    NOTE: SCL source line.

    80   else race= vname(list[whichn(max(of xx

  • ), of xx
  • )]);
  •   ------

    68

    ERROR: Undeclared array referenced: list.

    ERROR 68-185: The function WHICHN is unknown, or cannot be accessed.

    81   run;

    NOTE: The SAS System stopped processing this step because of errors.

    WARNING: The data set RAKSHI.TSK4 may be incomplete.  When this step was stopped there were 0

    observations and 3 variables.

    WARNING: Data set RAKSHI.TSK4 was not replaced because this step was stopped.

    NOTE: DATA statement used:

    real time           0.01 seconds

    cpu time            0.00 seconds

    Could You Help what the Exact Error is

    Regards,

    Sushil

    Super User
    Posts: 11,338

    Re: Array Function

    Posted in reply to SushilKumar

    You should post your whole code. The most likely reason is you are not declaring the array XX properly and/or you left the reference to the array LIST from Jagadish's example but no longer have the declaration for that array in your code.

    Contributor
    Posts: 32

    Re: Array Function

    Here My Whole Coding

    I Create dataset like

    data Rakshi.task4;

    input Studyid$ Subject$ Result$ Siteid A B C E F;

    datalines;

    R281017 1001 25 101 0 0 1 1 0

    R281017 1001 27 101 0 0 1 1 0

    R281017 1002 30 102 1 0 0 1 1

    R281017 1002 35 102 0 0 1 0 0

    R281017 1003 40 103 0 0 0 0 1

    run;

    Resulting dataset

    data Rakshi.tsk4(Keep = Usubjid Orres Race);

    set Rakshi.task4;

    siteid1 = put(siteid,??best.);

    Usubjid = trim(left(studyid))||trim(left(siteid1))||trim(left(subject));

    Orres = input(result,best.);

    array xx(*)a b c e f;

    Sums = sum(of xx(*));

    if sums > 1 then race='Multiple';

    else race= vname(xx[whichn(max(of xx

  • ), of xx
  • )]);
  • drop= sums;

    run;

    Trusted Advisor
    Posts: 1,137

    Re: Array Function

    Posted in reply to SushilKumar

    Hi Sushil,

    The problem is you are using sas version 8 and some of the functions i used  are not woring in 8 sas. i wrote the code in sas 9 and it worked well.

    Like the fuction whichn() i used will retrieve the variable position number from the array list. this is necessary to get the variable number and from the number the vname() will retrive the variable name from the array.

    Now our task is to find out the another function or alternative method to pull the variable number and and convert the number to variable name.

    Thanks,

    Jagadish

    Thanks,
    Jag
    Super Contributor
    Posts: 644

    Re: Array Function

    Posted in reply to Jagadishkatam

    Try (untested code)

         data want;

             set have;

             length Race $ 8 ;

             array list{*} a b c d e;

             k = 0 ;

             do i = 1 to dim(list) ;

                   if  list{i} > 0

                      then Race = varname(list{i}) ;

                   k + list{i} ;

             end ;

             if k > 1

                   then Race='Multiple';

        drop i k ;

    run;

    In this case when there is more than one, the variable Race is overwritten by 'Multiple'

    otherwise the name of a single variable will be returned

    Richard

    Message was edited by: Richard Carson:  clarification

    Solution
    ‎03-23-2013 12:20 PM
    Super User
    Posts: 5,496

    Re: Array Function

    Posted in reply to RichardinOz

    I like the idea, but consider that this might be a little simpler way to overwrite Race:

    do i = 1 to dim(list);

      if list{i} > 0 then do;

         if Race= ' ' then Race = vname(list{i});

         else Race = 'Multiple';

      end;

    end;

    drop i;

    Trusted Advisor
    Posts: 1,137

    Re: Array Function

    Posted in reply to Astounding

    Hi Richard and Astounding,

    Indeed your ideas were very useful.

    i just modified my earlier code in the below way and it worked

    data want;

        set have;

        array list(*) a b c d e;

        sums=sum(of list(*));

      do i=1 to dim(list);

        if sums > 1 then race='Multiple';

        if race = ' '  and list(i)=1 then race= vname(list(i));

      end;

        drop sums;

    run;

    Thanks,

    Jagadish

    Thanks,
    Jag
    🔒 This topic is solved and locked.

    Need further help from the community? Please ask a new question.

    Discussion stats
    • 8 replies
    • 1195 views
    • 6 likes
    • 5 in conversation