Help using Base SAS procedures

Excluding variables of varlist A from variables of varlist B

Accepted Solution Solved
Reply
Contributor Moh
Contributor
Posts: 20
Accepted Solution

Excluding variables of varlist A from variables of varlist B

Hi folks: 

I have the following varlists (_effectentered and _effectremoved). 

I want to have another varlist (_stdvar) which excludes &_effectremoved from &_effectentered.

I tried the following code but the problem is that x2 will be dropped from x23 as well.

 

%let _EffectEntered = x1 x2 x3 x23 ;
%let _EffectRemoved = x2 x3 ;
%let _stdvar=%sysfunc(prxchange(s/%sysfunc(translate(&_EffectRemoved,%str(|),%str( )))//,-1,&_EffectEntered));
%put &_stdvar ;

 

would you please help me in this regard?

Thanks


Accepted Solutions
Solution
‎01-10-2016 08:16 AM
Trusted Advisor
Posts: 1,115

Re: Excluding variables of varlist A from variables of varlist B

Hi Moh,

 

Good idea to use PRXCHANGE here, so you can perform this fairly complex operation within the %LET statement.

 

I think you should insert word boundary metacharacters and put the "or" expression in parentheses to make sure that only complete words are replaced, not substrings of longer words:

%let _stdvar=%cmpres(%sysfunc(prxchange(s/\b(%sysfunc(translate(&_EffectRemoved,%str(|),%str( ))))\b//,-1,&_EffectEntered)));
%put &_stdvar;

The %CMPRES function (or macro) replaces multiple blanks by single blanks.

View solution in original post


All Replies
Solution
‎01-10-2016 08:16 AM
Trusted Advisor
Posts: 1,115

Re: Excluding variables of varlist A from variables of varlist B

Hi Moh,

 

Good idea to use PRXCHANGE here, so you can perform this fairly complex operation within the %LET statement.

 

I think you should insert word boundary metacharacters and put the "or" expression in parentheses to make sure that only complete words are replaced, not substrings of longer words:

%let _stdvar=%cmpres(%sysfunc(prxchange(s/\b(%sysfunc(translate(&_EffectRemoved,%str(|),%str( ))))\b//,-1,&_EffectEntered)));
%put &_stdvar;

The %CMPRES function (or macro) replaces multiple blanks by single blanks.

Contributor Moh
Contributor
Posts: 20

Re: Excluding variables of varlist A from variables of varlist B

Thanks so much

Super User
Posts: 5,079

Re: Excluding variables of varlist A from variables of varlist B

Essentially, this will be the same problem as one that was recently answered:

 

https://communities.sas.com/t5/SAS-Data-Management/How-to-merge-two-varlists/m-p/242534

 

Assuming that it is OK to uppercase the variable names:

 

%let _stdvar=;

%let _EffectEntered = %upcase(&_EffectEntered);

%let _EffectRemoved = %upcase(&_EffectRemoved);

 

%do i=1 %to %sysfunc(countw(&_EffectEntered));

    %let nextone = %scan(&_EffectEntered, &i);

    %if %index( %str( &_EffectRemoved ), %str( &nextone )) = 0 %then %let _stdvar = &_stdvar &nextone;

%end;

 

The usual warnings apply.  You will need to define a macro since the code uses %IF.  In that case, you need to pay attention to which macro variables are local and which are global.

 

Good luck.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 312 views
  • 1 like
  • 3 in conversation