sasdatastep

Determining if a string macro variable does not exist in a column of a dataset in SAS


I have a string macro variable %let string=abc and a dataset column that contains values def ghi.

How do I use datastep to run &string. through the entire column to create an indicator macro variable to show that abc does not exist in the column?


Solution

  • You can use call symput() to create a macro variable in a data step.

    The code below creates a macro variable which has value 0 if the string is not found in the data, and value 1 if the string is found:

    * create example data;
    data have;
        input var1 $;
        datalines;
    def
    ghi
    ;
    run;
    
    %let string=abc;
    
    * data step to create variable STRING_CHECK which equals 1 if HAVE contains 'abc', and equals 0 otherwise;
    data _null_;
        set have (keep=var1) end=last_obs;
        retain flag 0;
        if var1 = "&string" then flag=1;
        * at end of data set, create the required macro variable;
        if last_obs then call symput('string_check',left(flag));
    run;
    
    %put &=string_check;