0
votes

I have a list of dataset variables in a macro variable called &allvars. I have another macro variable called &contlist that contains the variables in &allvars that are continuous. I'm try to extract the variables in &allvars which are not in &contlist, i.e. are not continuous.

This is what I have:

%let binlist = &allvars;
%let i = 1;

%do %until( %scan(&var, &i, %str( )) = );
    %let v = %scan(&var, &i, %str( ));

    %let j = 1;

    %do %until( %scan(&contlist, &j, %str( )) = );
        %let c = %scan(&contlist, &j, %str( ));

        %if %upcase(&v) = %upcase(&c) %then
            %let binlist = %sysfunc(tranwrd(&binlist, &v, %str()));

        %let j = %eval(&j + 1);
    %end;

    %let i = %eval(&i + 1);
%end;

%let binlist = %sysfunc(compbl(&binlist));

However, this causes a problem when variables have similar names, like so:

%let allvars = x x1 x2 x3;
%let contlist = x x1 x2;

The desired output here would be only x3, but instead this produces 1 2 3 because of the call to TRANWRD.

My only other thought would be to output both lists to datasets, merge them, keep only the records from allvars not in contlist, then create binlist from those records. However, there has to be a simpler, more elegant solution.

Any ideas?

1
Example #1435 of why you shouldn't store data (even metadata) in macro variables except to directly use.Joe
@Joe: I'm revamping the internals of a macro that I didn't write. Those lists are macro parameters. Not my choice.Alex A.

1 Answers

1
votes

There isn't a good solution for your problem-as-stated. There are some messy ones (more messy than your provided one, which I consider pretty messy). Perl regular expression is probably your best bet.

Ultimately, the right answer is to figure this out from datasets. Whether that means you create a dataset with your macro variable (messy, sure), or store this in the first place in datasets and pull out the pieces you need for various purposes (the right way to do it), that's the best solution. Macro code like this is messy, error-prone, easily harmed by changes, and hard to read. Joining two datasets to each other will get you the right answer pretty quickly with a low error rate, will be obvious to even a fairly novice programmer, and be less of a headache overall.