Lukas Halim
Lukas Halim

Reputation: 1525

Embed SAS Macro Results in SAS code

I would like to include the results of a macro function call in a data step. I can do this indirectly, by first assigning the macro function output to a macro variable, and then using that macro variable within my function, but this seems inelegant.

data dataset_employee;
input name $ dept $;
  datalines;
  John Sales
  Mary Acctng
;
data dataset_employee;
input name $ dept $;
  datalines;
  John Sales
  Mary Acctng
;

data dataset_manager;
input name $ dept $;
  datalines;
  Will Sales
  Sue Acctng
;

It seems like SAS doesn't realize that the macro call is complete and I'm switching to regular SAS code.

 /*this works*/
%let var = %DO_OVER(VALUES=employee, PHRASE=dataset_?) dataset_manager; 
data combined1;
  set &var dataset_manager;
run;

/*this fails*/
data combined;
  set %DO_OVER(VALUES=employee manager, PHRASE=dataset_?); 
  dataset_manager;
run;

/*this works*/
data combined;
  set dataset_manager %DO_OVER(VALUES=employee manager, PHRASE=dataset_?); 
  ;
run;

Can anyone help me understand what is going on here?

Upvotes: 1

Views: 337

Answers (1)

DaBigNikoladze
DaBigNikoladze

Reputation: 661

It seems that the failing attempt is due to an extra ; at the end of the macro invocation. Try removing it.

A macro call doesn't require a semicolon. The first example works without a semicolon after the macro call (pay attention, you are using the dataset_manager dataset twice, in the %let and again in the set statement).

The third example would work even if you remove one of the two semicolons (one is required to end the set statement).

Upvotes: 5

Related Questions