DATA Step, Macro, Functions and more

how to find if a variable is in an array

Reply
SAS Employee kwu
SAS Employee
Posts: 34

how to find if a variable is in an array

here is what I want to do:
I have a list of numbers stored in a macro variable, I want to find out if this list of numbers in the macro variables are all the ids or a subset of ids in a dataset.

I saw there is a vinarray function in sas help but the example is not well documented, I do not understand how it would work.

of course, I could use scan to get a id at a time and then to look it up in the dataset but I think there got be a easier way.


any suggestion? thank you very much in advance.
Super Contributor
Posts: 647

Re: how to find if a variable is in an array

can you send an example.
Super Contributor
Super Contributor
Posts: 3,174

Re: how to find if a variable is in an array

For me, it's unclear if you are concerned about SAS variables or SAS variable "values"? I see a reasonable example in the SAS 9.2 DOC on VINARRAY function - here:

http://support.sas.com/documentation/cdl/en/lrdict/63026/HTML/default/viewer.htm#a000245979.htm


Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

vinarray site:sas.com
SAS Employee kwu
SAS Employee
Posts: 34

Re: how to find if a variable is in an array

thank you for the example. then vinarray would not meet my need since it checks if the variable is in the arrray. I want to check if the value is in the array.
Super Contributor
Super Contributor
Posts: 3,174

Re: how to find if a variable is in an array

You will need to parse each sub-field in "B" and check for its value in "A", or the opposite, as you require. The process can done with either SAS macro language using %SCAN and %SYSFUNC(INDEX(argument1,argument2)) or with a DATA step, using the similar CALL function SCAN and INDEX(argument1,argument2).

Scott Barry
SBBWorks, Inc.
SAS Employee kwu
SAS Employee
Posts: 34

Re: how to find if a variable is in an array

index would not work since

index("1 23 3", "2") will return 3, which is not what I want.
SAS Employee kwu
SAS Employee
Posts: 34

Re: how to find if a variable is in an array

I guess I can trick it by doing
index ("1 23 3", "2 ");

but I really wish there is some sas function out there which can look for a number rather than just chars.
Respected Advisor
Posts: 3,777

Re: how to find if a variable is in an array

You can treat them as WORDS using INDEXW.

Or you can use WHICHN function as in this example.

[pre]
1612 %let id_list1=%str(1 23 2 3);
1613 %let id_list2=%str(1 2 3 4);
1614
1615 data _null_;
1616 array _a[%sysfunc(countw(&id_list1))] (&id_list1);
1617 array _b[%sysfunc(countw(&id_list2))] (&id_list2);
1618 do i = 1 to dim(_a);
1619 f = whichN(_a,of _b
  • );
    1620 if f eq 0 then put 'NOTE: The value ' _a 'from id_list1 is not in id_list2.';
    1621 end;
    1622 run;

    NOTE: The value 23 from id_list1 is not in id_list2.
    [/pre] Message was edited by: data _null_;
  • SAS Employee kwu
    SAS Employee
    Posts: 34

    Re: how to find if a variable is in an array

    perfect, that is what I want exactly.
    thank you so much, Smiley Happy

    have a good weekend.
    SAS Employee kwu
    SAS Employee
    Posts: 34

    Re: how to find if a variable is in an array

    one more question, is there way to break the do loop when I see a unmatch, since once a unmatch found, there is no need to do the rest check any more.
    Respected Advisor
    Posts: 3,777

    Re: how to find if a variable is in an array

    LEAVE;

    [pre]
    %let id_list1=%str(1 23 2 3);
    %let id_list2=%str(1 2 3 4);

    data _null_;
    array _a[%sysfunc(countw(&id_list1))] (&id_list1);
    array _b[%sysfunc(countw(&id_list2))] (&id_list2);
    do k = 1 to dim(_a);
    f = whichN(_a,of _b
  • );
    if f eq 0 then do;
    put 'NOTE: The value ' _a 'from id_list1 is not in id_list2.';
    leave;
    end;
    end;
    put 'NOTE: stopped at ' K=;
    run;
    [/pre]
  • SAS Employee kwu
    SAS Employee
    Posts: 34

    Re: how to find if a variable is in an array

    thank you, data _null_.

    I see correct the sample code, _a even, Smiley Happy
    SAS Employee kwu
    SAS Employee
    Posts: 34

    Re: how to find if a variable is in an array

    for example,

    %let id_list1=%str(1 23 2 3);
    %let id_list2=%(str(1 2 3 4);
    I wonder if there is an efficient way to determine that 23 is not in id_list2 .
    Ask a Question
    Discussion stats
    • 12 replies
    • 222 views
    • 0 likes
    • 4 in conversation