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

Hi I have below program,

 

%let cri=abc in ('ABD' ' AND AND' 'PAND-AND AND' ' AND -AND' 'ABC') AND def in ('ABD' 'AND AND' 'PAND-AND AND' 'AND -AND' 'ABC');
%let QUERY_CRITERIA_CMPRES=%sysfunc(compress(&cri.,()));
data _null_;
call symputx("QUERY_CRITERIA_PIPE",tranwrd("&QUERY_CRITERIA_CMPRES.","' AND ","'|"));
run;

           

 

%put &=QUERY_CRITERIA_PIPE;

its working for outside of single quote AND which is required but it is also replacing AND which is inside quote, however i need to replace AND with pipe '|' only which are not in single quote. Anyone help?

Expected output-

abc in ('ABD' ' AND AND' 'PAND-AND AND' ' AND -AND' 'ABC') | def in ('ABD' ' AND AND' 'PAND-AND AND' ' AND -AND' 'ABC');

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
%let cri=abc in ('ABD' ' AND AND' 'PAND-AND AND' ' AND -AND' 'ABC') AND def in ('ABD' 'AND AND' 'PAND-AND AND' 'AND -AND' 'ABC');

data have;
have="&cri";
length want $ 200;
do i=1 to countw(have,"'",'m');
 if mod(i,2)=0 then want=catx(' ',want,quote(scan(have,i,"'",'m'),"'"));
 if mod(i,2)=1 then want=catx(' ',want,prxchange('s/AND/|/i',-1,scan(have,i,"'",'m')));
 end;
call symputx('want',want);
run;

%put &=cri;
%put &=want ;

View solution in original post

3 REPLIES 3
Ksharp
Super User
%let cri=abc in ('ABD' ' AND AND' 'PAND-AND AND' ' AND -AND' 'ABC') AND def in ('ABD' 'AND AND' 'PAND-AND AND' 'AND -AND' 'ABC');

data have;
have="&cri";
length want $ 200;
do i=1 to countw(have,"'",'m');
 if mod(i,2)=0 then want=catx(' ',want,quote(scan(have,i,"'",'m'),"'"));
 if mod(i,2)=1 then want=catx(' ',want,prxchange('s/AND/|/i',-1,scan(have,i,"'",'m')));
 end;
call symputx('want',want);
run;

%put &=cri;
%put &=want ;
Tushar
Obsidian | Level 7
Thanks it worked 🙂
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1147 views
  • 1 like
  • 2 in conversation