passing variable values to macro in data step

Reply
Contributor
Posts: 36

passing variable values to macro in data step

Hi,

How do I pass the variable values into the macro? I have the following sample code. What I need is to print out the number of lines that is defined by 'times' in the 'test_data'. Thank you very much.

%macro test(finish);

     %let i=1;

     %do %while (&i<&finish);

          %put the value of i is &i;

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

     %end;

%mend test;

data test_data;

     INPUT times;

     %test(times);

datalines;

3

5

;

Super User
Posts: 19,770

Re: passing variable values to macro in data step

Call Execute, see the second example in the documentation:

SAS(R) 9.2 Macro Language: Reference

Contributor
Posts: 36

Re: passing variable values to macro in data step

Thank you so much!

Super User
Posts: 11,343

Re: passing variable values to macro in data step

What do you expect the output to look like? May not need any macro code at all.

Respected Advisor
Posts: 3,156

Re: passing variable values to macro in data step

Or DOSUBL(), If you have SAS 9.3 M2 or later.

SAS(R) 9.4 Functions and CALL Routines: Reference, Second Edition

Please note, DOSUBL() is different from CALL EXECUTE(). The latter is done after the data step, while the DOSUBL() is done during the data step, so it is capable "PASS" a value back to data step literally. 

Haikuo

Valued Guide
Posts: 2,177

Re: passing variable values to macro in data step

Why is this a macro?

the objective can be delivered by the datastep in which it is invoked

Contributor
Posts: 36

Re: passing variable values to macro in data step

Thank you for the reply. My original macro was actually a more complicated one. While debugging that macro, I encountered a problem involving passing variable values into macros during data step. Therefore, I created a simple sample for this question.

Ask a Question
Discussion stats
  • 6 replies
  • 306 views
  • 2 likes
  • 5 in conversation