DATA Step, Macro, Functions and more

how to delete blank lines in SAS code?

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

how to delete blank lines in SAS code?

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


Accepted Solutions
Solution
‎09-12-2011 02:20 PM
Super User
Super User
Posts: 6,500

how to delete blank lines in SAS code?

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


All Replies
PROC Star
Posts: 7,363

how to delete blank lines in SAS code?

Blank lines within a program or within a macro variable?

Contributor
Posts: 70

how to delete blank lines in SAS code?

within a program. thanks.

PROC Star
Posts: 7,363

how to delete blank lines in SAS code?

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.

Contributor
Posts: 70

how to delete blank lines in SAS code?

Thanks art297.

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

Super Contributor
Posts: 356

Re: how to delete blank lines in SAS code?

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

Barry

Solution
‎09-12-2011 02:20 PM
Super User
Super User
Posts: 6,500

how to delete blank lines in SAS code?

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;

Contributor
Posts: 70

how to delete blank lines in SAS code?

Thansk Tom.

This code works perfectly in Windows.

Trusted Advisor
Posts: 1,300

how to delete blank lines in SAS code?

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

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

Contributor
Posts: 70

how to delete blank lines in SAS code?

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

Trusted Advisor
Posts: 1,300

how to delete blank lines in SAS code?

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.

☑ This topic is SOLVED.

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

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