BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Moh
Obsidian | Level 7 Moh
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

3 REPLIES 3
FreelanceReinh
Jade | Level 19

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.

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1151 views
  • 2 likes
  • 3 in conversation