BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

is there a way to match %do and %end in a spaghetti code? I'm lost finding the %end statements for all %do's

2 REPLIES 2
SASKiwi
PROC Star

I don't think there is any automated way to do this. You will just have to manually search the code for the %DO-%END blocks, then try to improve readability by indenting the blocks so they easier to spot. You could try Enterprise Guide's code formatter to speed things up but then you will have to put up with EG's formatting rules if they are different to yours.

 

It is a good object lesson for avoiding spaghetti coding.

FreelanceReinh
Jade | Level 19

Hi @SASPhile,

 

I've just created a keyboard macro (for the SAS Enhanced Editor) which seems to do what you want, but it's rather a risky workaround (with limitations) than a clean solution.

 

The major limitation is: Your code must not contain both curly braces {} and square brackets [].

 

Let's assume it doesn't contain curly braces (then it may contain square brackets).

 

I recorded a keyboard macro (Enhanced Editor: Tools --> Keyboard Macros --> Record New Macro) which replaces %do ("Match whole word only") with { and %end with } ("Replace All") and vice versa (so as to restore the original code). Then I stopped recording, edited the macro (Tools --> Keyboard Macros --> Macros... --> Edit...) to insert the command "Move cursor to matching brace/paren" in the middle between the replacement actions and assigned a shortcut key to the macro.

 

Now, when I place the cursor on the percent sign of a %do or %end keyword and press the shortcut key, the cursor jumps to the percent sign of the matching %end or %do, respectively (and back if I press the shortcut key again).

 

Obviously, I haven't tested this macro thoroughly yet and I wouldn't really recommend it.

 

Given that there is an Enhanced Editor command to find matching DO/END pairs in a data step, it's a pity that there is no similar feature for %DO/%END.

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1399 views
  • 0 likes
  • 3 in conversation