DATA Step, Macro, Functions and more

SAS Macros

Accepted Solution Solved
Reply
PROC Star
Posts: 554
Accepted Solution

SAS Macros

[ Edited ]

Hi

 

Using SAS Enterprise Guide 7.1, is there any what that I can make SAS recognize data steps eg. in the context of text formatting?

That is, is there any way that I can make the word 'data' inside my macro definition below dark blue as in the data step outside the macro definition? And the same for the words 'set' and 'where' and so on?

 

I realize that macros are essentially just replacement of text, but I am wondering if it is possible.

 

Thank you in advance Smiley Happy

 

data mydata;
	set sashelp.cars;
	where cylinders = 10;
run;

%macro make_dataset(inDS = );
	data mydata;
		set &inDS;
		where cylinders = 10;
	run;
%mend make_dataset;

 

Regards

 

EDIT: I noticed that the inserted code here does exactly what I want to do, but my Enterprise guide however does not. (See snip)

 

Udklip.PNG


Accepted Solutions
Solution
‎04-04-2017 06:46 AM
Super User
Super User
Posts: 7,430

Re: SAS Macros

Its not that I am not fond of macro, it is a very useful tool, its just that most of the time its either used as a replacement for knowledge of Base SAS, or cobbling together very messy code.  Probably what your experiencing with these "quite a few".  If its some sort of library of macros or set of them, then this is where SDLC should come in (and it is a good use for macro).  Design macros with functional design specs, testing, user manual etc.  Then the actual code itself is irrelevant.  Unfortunately most of the time this is either not done, or a small part is doen some time after development.

 

Anyhows, back to the question.  The editor recorgnises the "&" in that code triggering the code highlighting - it can do this anywhere as the & is fixed, so if you use that symbol followed by a non-numeric or missing character is assumes its a macro variable.  Maybe this code will illustrate why it can't highlight free text:

%macro somemacro ();
  data will go here
%mend;

data _null_;
  put "%somemacro";
run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,430

Re: SAS Macros

No.  And the reason is that which you have given yourself.  Macro is nothing more that a text find/replace system, and as such takes the text within the macro and mend statements as plain text, resolves anything within the macro pre-processor, and then replaces that in the code text file and send to the compiler.  You can put anything you like in there, and until it passes through the macro pre-processor you will not know exactly what it will look like.  Use macro sparingly, and only when it adds value to the code and you will find this is not an issue as you will use Base SAS 99% of the time.

PROC Star
Posts: 554

Re: SAS Macros

Thank you for your answer. I am not fund of the macro laguage myself and I try to avoid it whenever I can. However I am in a situation where I have to use and maintain quite a few of them. It confuses me a bit though, that EG 7.1 can't do this when it can highligt character, macro variables and numbers though? Smiley Happy

 

Udklip.PNG

Solution
‎04-04-2017 06:46 AM
Super User
Super User
Posts: 7,430

Re: SAS Macros

Its not that I am not fond of macro, it is a very useful tool, its just that most of the time its either used as a replacement for knowledge of Base SAS, or cobbling together very messy code.  Probably what your experiencing with these "quite a few".  If its some sort of library of macros or set of them, then this is where SDLC should come in (and it is a good use for macro).  Design macros with functional design specs, testing, user manual etc.  Then the actual code itself is irrelevant.  Unfortunately most of the time this is either not done, or a small part is doen some time after development.

 

Anyhows, back to the question.  The editor recorgnises the "&" in that code triggering the code highlighting - it can do this anywhere as the & is fixed, so if you use that symbol followed by a non-numeric or missing character is assumes its a macro variable.  Maybe this code will illustrate why it can't highlight free text:

%macro somemacro ();
  data will go here
%mend;

data _null_;
  put "%somemacro";
run;
PROC Star
Posts: 554

Re: SAS Macros

This makes sense. Thank you very much @RW9.

 

Regards

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 138 views
  • 1 like
  • 2 in conversation