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

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 ;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

1 REPLY 1
Tom
Super User Tom
Super User

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 ;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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