BookmarkSubscribeRSS Feed
kwu
SAS Employee kwu
SAS Employee
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.
12 REPLIES 12
SASPhile
Quartz | Level 8
can you send an example.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
kwu
SAS Employee kwu
SAS Employee
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
kwu
SAS Employee kwu
SAS Employee
index would not work since

index("1 23 3", "2") will return 3, which is not what I want.
kwu
SAS Employee kwu
SAS Employee
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.
data_null__
Jade | Level 19
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_;
  • kwu
    SAS Employee kwu
    SAS Employee
    perfect, that is what I want exactly.
    thank you so much, 🙂

    have a good weekend.
    kwu
    SAS Employee kwu
    SAS Employee
    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.
    data_null__
    Jade | Level 19
    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]
  • kwu
    SAS Employee kwu
    SAS Employee
    thank you, data _null_.

    I see correct the sample code, _a even, 🙂
    kwu
    SAS Employee kwu
    SAS Employee
    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 .

    SAS Innovate 2025: Register Now

    Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
    Sign up by Dec. 31 to get the 2024 rate of just $495.
    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
    • 12 replies
    • 2108 views
    • 0 likes
    • 4 in conversation