SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Dynamic Search with FIND/INDEX

Accepted Solution Solved
Reply
Contributor
Posts: 20
Accepted Solution

Dynamic Search with FIND/INDEX

With this code I intend to do a dynamic search in Name field and enter the search term (a macrovariable sn_term&i) if found into NewN. I am having issues with the outputing the results; the last search term tend to overwrite the previous ones. Thanks in advance.


%macro mstrial;

    data  newlbname;

    set  SmplEHM;

    %let i=1;

    %Do %until(&i>&p);

    select;

        when ((find(Name,"&&sn_term&i"))GE 1)

        %let Mvname= &&sn_term&i;

        NewN = strip(symget('Mvname'));

        otherwise ;

        end;

     %Let i=%eval(&i+1);

    %end;

    run;

    %mend ;


Accepted Solutions
Solution
‎02-01-2014 04:59 PM
Super User
Super User
Posts: 7,039

Re: Dynamic Search with FIND/INDEX

Do not use SYMGET() as it will run after all of the macro code has finished so it will always retrieve the last value assigned to MVNAME.

Also you seem to be generating multiple SELECT statements instead of multiple WHEN statements within a single SELECT statement.

You probably want something like this.

%macro mstrial ;

data  newlbname;

  set  SmplEHM;

  select;


  %do i=1 %to &p ;

    %let Mvname= &&sn_term&i;

    when ((find(Name,"&mvname"))GE 1)

          NewN = "&mvname"

    ;

  %end;


    otherwise ;

  end;

run;

%mend ;

View solution in original post


All Replies
Solution
‎02-01-2014 04:59 PM
Super User
Super User
Posts: 7,039

Re: Dynamic Search with FIND/INDEX

Do not use SYMGET() as it will run after all of the macro code has finished so it will always retrieve the last value assigned to MVNAME.

Also you seem to be generating multiple SELECT statements instead of multiple WHEN statements within a single SELECT statement.

You probably want something like this.

%macro mstrial ;

data  newlbname;

  set  SmplEHM;

  select;


  %do i=1 %to &p ;

    %let Mvname= &&sn_term&i;

    when ((find(Name,"&mvname"))GE 1)

          NewN = "&mvname"

    ;

  %end;


    otherwise ;

  end;

run;

%mend ;

🔒 This topic is solved and locked.

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

Discussion stats
  • 1 reply
  • 244 views
  • 0 likes
  • 2 in conversation