Search code examples
sas

Dynamically remove variables containing a value


suppose to have the following:

    hospital      region     place     exemption      low      
    NotValue      NotValue    30           3        NotValue      

I have totally 100 variables. I would like to remove all variables containing "NotValue" dynamically in the sense that I cannot list all the variable names. Is there an easy way to do this?

Thank you in advance


Solution

  • One way is to iterate through all of your columns and save the variable name into a space-separated list every time a variable's value is "NotValue." You can pass this into a macro variable and create your desired dataset with another data step.

    data _null_;
        set have;
        array charvars[*] _CHARACTER_;
        length dropvars $3200.;
    
        do i = 1 to dim(charvars);
            if(charvars[i] = 'NotValue') 
                then dropvars = catx(' ', dropvars, vname(charvars[i]));
        end;
    
        call symputx('dropvars', dropvars);
    run;
    
    data want;
        set have;
        drop &dropvars;
    run;
    
    place     exemption      
    30        3