BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Fisher
Quartz | Level 8

I have a long SAS macro, and there are lots of blank lines within codes which I like to delete.

Anybody know how to do that trick? thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Depends on the tools you have.  You could use sed or other unix tools from the command line.

Not that hard to do with a data step.

data _null_;

  infile 'old.sas' lrecl=1000;

  file 'new.sas' lrecl=1000;

  input;

  if _infile_=' ' then delete;

* Why not remove those pesky trailing blanks some editors insert while we are at it. ;

  len= length(_infile_);

  put _infile_ $varying1000. len ;

run;

View solution in original post

10 REPLIES 10
art297
Opal | Level 21

Blank lines within a program or within a macro variable?

Fisher
Quartz | Level 8

within a program. thanks.

art297
Opal | Level 21

Actually there is a way to do it in SAS, but a little difficult to explain.

SAS has what are call keyboard macros and abbreviations (they are the same thing).  You can record your own by clicking on tools->record macro.

Once one is recorded, it can be edited.

Thru a combination of starting a macro to record, clicking on select all and find and entering what I wanted SAS to find, and then stopping and naming the macro, I was able to edit the macro to contain the following:

Select all

Regenerate leading whitespace

Remove trailing whitespace

Reset search

Set search direction to forward

Set search string to "^\n"

Set search to include comments

Set search to search code

Delete line

Replace all

Reset search

That took all of about 30 seconds to create and I was able to assign the macro the a key (e.g., alt-c)

What the code is doing is removing white space in case you have blank looking lines, using a regular expression to find carriage returns at the start of lines, delete those lines once found.

In short, after creating the macro and assigning the key, pressing alt-c removed all blank lines.

Let me know if you can't figure out how to do it as keyboard macros have another benefit .. they can be exported!  In short, I could easily send you the abbreviation and you could import it.

Fisher
Quartz | Level 8

Thanks art297.

I followed some of your explaination, but I really glad to find this features in SAS. I will try later again.

twocanbazza
Quartz | Level 8

You could also us the 'Delete' or 'Backspace' key on your keyboard Smiley Happy Smiley Wink

Barry

Tom
Super User Tom
Super User

Depends on the tools you have.  You could use sed or other unix tools from the command line.

Not that hard to do with a data step.

data _null_;

  infile 'old.sas' lrecl=1000;

  file 'new.sas' lrecl=1000;

  input;

  if _infile_=' ' then delete;

* Why not remove those pesky trailing blanks some editors insert while we are at it. ;

  len= length(_infile_);

  put _infile_ $varying1000. len ;

run;

Fisher
Quartz | Level 8

Thansk Tom.

This code works perfectly in Windows.

FriedEgg
SAS Employee

if you are using unix you can do the following via the shell:

tr -s \n old.sas > new.sas

Fisher
Quartz | Level 8

I tried this command in Linux, but it only generated a blank new.sas.

FriedEgg
SAS Employee

First the program would need to utilize unix style EOL characters or \n would need to properly identify the correct EOL.  You can preprocess by converting from dos2unix

dos2unix mymacro.sas

tr -s "\n" mymacro.sas > mymacro_new.sas

tr will squeeze (-s) duplicate apperances of the EOL char ("\n") and leave single apperances intact.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 4422 views
  • 3 likes
  • 5 in conversation