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
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;
Blank lines within a program or within a macro variable?
within a program. thanks.
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.
Thanks art297.
I followed some of your explaination, but I really glad to find this features in SAS. I will try later again.
You could also us the 'Delete' or 'Backspace' key on your keyboard
Barry
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;
Thansk Tom.
This code works perfectly in Windows.
if you are using unix you can do the following via the shell:
tr -s \n old.sas > new.sas
I tried this command in Linux, but it only generated a blank new.sas.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.