DATA Step, Macro, Functions and more

How to return specific macro variable result to the macro call

Reply
Occasional Contributor
Posts: 13

How to return specific macro variable result to the macro call

Can you anyone tell me how to get the macro variable(_stat_desc) result to the macro call.

I dont want to get the data steps in the macro call[You can view the result in the dataset after macro call]

The only difference is having additional data step which causes the Error.

Here the two programs given below for your reference.

Program 1 is present with error i need to rectify. [Just like Program 2 result i am expecting]

/*Program 1. (Error)*/

%macro stat_desc11();

    *** Data step;

    data _null_;

/*        put "Error Found";*/

    run;

    %let _stat_desc=%str(STAT DESCRIPTION);

    &_stat_desc;

%mend stat_desc11;

%put %stat_desc11;

/*For your reference:;*/

data test;

      x="%str(%stat_desc11)";

      put x=;

run;

/*Program 2*/

%macro stat_desc11();

    %let _stat_desc=%str(STAT DESCRIPTION);

    &_stat_desc;

%mend stat_desc11;

%put %stat_desc11;

/*For your reference:;*/

data test;

      x="%str(%stat_desc11)";

      put x=;

run;

Alternate way of solution is also is acceptable,

Thanks in advance,

Thanks,

Kuthubudeen

Super User
Posts: 5,256

How to return specific macro variable result to the macro call

First, you can't use data step/proc logic in in-line macros.

Second, macros are more sensitive to commenting, especially in in-line macros. So if you comment, try use macro commenting. /* ... */ may also work (at least better than *...; ).

It's hard from your example to see what you are trying to accomplish, so it's hard to give you any other solution.

/Linus

Data never sleeps
Occasional Contributor
Posts: 13

How to return specific macro variable result to the macro call

Thanks for your quick response Linus,

Before that I just confirm that inline macro means "The macro which will return some value to the macro call"

Please correct me if my assumption is wrong.

My expectation is very simple. I am trying to return some macro variable result to the macro call. In that macro definition i used some data steps.

Is there any alternate way to return variable value to the macro call.[Except using macro parameter].

Thanks,

Kuthubudeen

Valued Guide
Posts: 2,175

Re: How to return specific macro variable result to the macro call

Kuthubudeen

the macro language delivers to the normal submitted language compiler anything it does not regard as syntax that it should execute.

So, if you wish a macro "to return a variable value", everything else must be "consumed by the macro language environment".

Nothing other than the "returned value" must be generated by the macro for the normal submitted language compiler (especially no semi-colons which interrupt statement execution - so no data steps nor proc steps).

The macro language executes things like %substr() and %let and almost all syntax starting with %something. The best known exceptions to this rule are %include and %inc.

Some data step activity might be able to be implemented in the macro language by using %sysfunc() and %syscall, but these exclude many data handling activities which mean that you cannot always have your macro "return a variable value".

good luck

peterC

Occasional Contributor
Posts: 13

Re: How to return specific macro variable result to the macro call

Hi peter,

Thank you very for your brief explanation. Now i understood thoroughly from your explanation.

Thanks,

Kuthubudeen

Super User
Posts: 5,256

Re: How to return specific macro variable result to the macro call

Your example does not have any parameters in the call.

If this is the case in your real world problem you can call the macro before the data step, have the macro create a global macro variable, and use that in your data step.

Not as neet as you may wished for, but it would work.

Linus

Data never sleeps
Ask a Question
Discussion stats
  • 5 replies
  • 211 views
  • 3 likes
  • 3 in conversation