Desktop productivity for business analysts and programmers

How to check if a macro variable contains certain strings?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

How to check if a macro variable contains certain strings?

 

/* sample dataset */
data have;
  infile datalines dlm=",";
  input date $ value;
datalines;
05AUG2010,5
;
run;


/* macro to calculate percentage */
%macro test11;
%global valueMax;
%let valueMAX = 10;

data WORK.have_adj;
set WORK.have;

format perc best8.;
perc = value/&valueMAX.;
run;

%mend test11;


/* macro to execute */
%let dataName = test;
%macro runTEST;

 %if %sysfunc(find("&dataName.",'test')) ge 1 %then %do;   /* something wrong here */
    %test11; 
    %end; 

%mend runTEST;
%runTEST;

/* check whether macro 'test' has been executed */
%put _ALL_;
run;

 

 The result suggests the macro 'test11' has not been executed. How to fix it?


Accepted Solutions
Solution
‎07-16-2017 11:19 PM
Super User
Posts: 19,192

Re: How to check if a macro variable contains certain strings?

Remove the quotes from the macro variable in the FIND function. 

Modified your code for testing to make it easier:

 

%macro runTEST(dataName);

 %if %sysfunc(find(&dataName.,test)) ge 1 %then %do;   /* something wrong here */
    %put FOUND TEST;
    %end; 
   %else %do;
   	%put NO TEST;
   %end;

%mend runTEST;
%runTEST(test);
%runTEST(hello_test123);
%runTEST(random);

View solution in original post


All Replies
Solution
‎07-16-2017 11:19 PM
Super User
Posts: 19,192

Re: How to check if a macro variable contains certain strings?

Remove the quotes from the macro variable in the FIND function. 

Modified your code for testing to make it easier:

 

%macro runTEST(dataName);

 %if %sysfunc(find(&dataName.,test)) ge 1 %then %do;   /* something wrong here */
    %put FOUND TEST;
    %end; 
   %else %do;
   	%put NO TEST;
   %end;

%mend runTEST;
%runTEST(test);
%runTEST(hello_test123);
%runTEST(random);
Frequent Contributor
Posts: 82

Re: How to check if a macro variable contains certain strings?

Thank you @Reeza for both the answer and tips! That solves the problem. 

Super User
Posts: 9,878

Re: How to check if a macro variable contains certain strings?

%macro runTEST(dataName);

 %if %index(&dataName.,test) %then %do;   /* something wrong here */
    %put FOUND TEST;
    %end; 
   %else %do;
   	%put NO TEST;
   %end;

%mend runTEST;
%runTEST(test)
%runTEST(hello_test123)
%runTEST(random)
☑ This topic is solved.

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

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