DATA Step, Macro, Functions and more

Can I call a macro process which builds a dataset inside RESOLVE?

Reply
Occasional Contributor
Posts: 7

Can I call a macro process which builds a dataset inside RESOLVE?

Hi Experts,

Hope you could enlighten me with this. I have a quite challenging problem using macros. Is it possible to call a procedure inside the sql? I think it will be better to show it in an example.

PROGRAM A:

%macro a;

%let test_value = test_val;

&test_value

%mend a;

data sample;

msg = "this is a test";

run;

proc sql;

create table a as

select msg, RESOLVE('%a') as val

from sample

;

quit;

proc print data = a;

run;

The output of these codes is the ff:

MSG                     VAL

this is a test              test_val



So as I understand, RESOLVE uses the value in %a. But what if aside from value resolution, an exception handling procedure is also embedded? Is this possible? I tried doing this code:


PROGRAM B:

%macro a;proc_,

%let test_value = test_val;

&test_value

%if &test_value = test_val %then %do;

data excep_dat;

val= “There is data exception.”;

run;

%end;

%mend a;


Supposed to be, the expected output is that it will also produce a dataset named excep_dat.

But what the output is shows is:


   Obs         msg                                      val

     1     this is a test    test_val data excep_dat; val= “There is data exception.”; run;


Is there a way to allow the production of the dataset? Or if this procedure is not valid, hope you could confirm so that we can think of another step to handle it. Thanks!


Frequent Contributor
Posts: 139

Re: Can I call a macro process which builds a dataset inside RESOLVE?

Hi,

macro just substitute the resolved program as text in your macro call.in the first instance when you invoke the macro in proc sql it substitute the macro program text which is text_val in the place where you invoke the macro.

However in second the macro program will resolve to may statement including data step which is not valid in proc sql.

you can write proc sql to achieve your result conditinally.

Super User
Posts: 10,020

Re: Can I call a macro process which builds a dataset inside RESOLVE?

No. Resolve() can't do that.If you want to make different datasets during reading a dataset ,use Call execute(),

which can generate dynamic code ,and these code is executed after the data step.

Ksharp

Super Contributor
Posts: 349

Re: Can I call a macro process which builds a dataset inside RESOLVE?

Hi,

Try this...hope this helps..

%macro a;

%let test_value = test_val;

This is a test;

%mend a;

data sample;

msg = "this is a test";

run;

data two;

set sample;

x=resolve('&test_value');

y=resolve('%a');

run;

or

proc sql;

create table a as

select msg, RESOLVE('&test_value') as val,Resolve('%a') as val2

from sample

;

quit;

Thanks,

Shiva

Ask a Question
Discussion stats
  • 3 replies
  • 171 views
  • 0 likes
  • 4 in conversation