pass values from macros to variables in data step

Reply
Contributor
Posts: 36

pass values from macros to variables in data step

Hi,

I have another question about how to pass values from macros to variables in data step. I tried both 'call execute' and 'dosubl' as instructed by the replies in my other post for passing variable values into macros. I got error messages for both statements.

The following is a simple sample code just for describing my question. I want to create a new variable according to the values of 'times' in data 'test_data'. Thanks a lot for your input.

%macro test(finish);

     %if (&finish > 4) %then new = 'great than 4';

%mend test;

data test_data;

     INPUT times;

     call execute('%test('||times||')');

     rc = dosubl'%test('||times||')');

datalines;

3

5

;




Super User
Super User
Posts: 7,055

Re: pass values from macros to variables in data step

What is it that you actually want to do?  The macro that you wrote is one that will just generate most of an assignment statement that could be used in a data step. Note that it is missing the semi-colon that would mark the end of the assignment statement as the semi-colon that is there is the one that ends the %IF %THEN statement.

If you just want to mimic the behavior of a FORMAT then just have the macro generate the string that you want and then use %RESOLVE() to call it and return the string that it generates.

%macro test(finish);

  %if (&finish > 4) %then great than 4 ;

%mend test;

data test_data;

  INPUT times;

  test = resolve(cats('%test(',times,')'));

  put times= test=;

datalines;

3

5

;

Super User
Super User
Posts: 7,055

Re: pass values from macros to variables in data step

Now if you want to test if you can have a macro that does actual data and proc calls then here is a better example.

In this case the macro stores the result into a global macro variable that the data step can then retrieve with SYMGET() function.

%macro test(finish);

data _null_;

   length new $200;

   if (&finish = 4) then new='equal to 4';

   else if (&finish > 4) then new='greater than 4';

   else new='less than 4';

   call symputx('result',new,'G');

run;

%mend test;

data test_data;

do times=3 to 5;

     rc = dosubl(cats('%test(',times,')'));

     result=symget('result');

     put (_all_) (=);

end;

run;

Contributor
Posts: 36

Re: pass values from macros to variables in data step

Thank you very much, Tom! Your replies help me understanding SAS Macro much better. I really appreciate it!

Ask a Question
Discussion stats
  • 3 replies
  • 255 views
  • 8 likes
  • 2 in conversation