Sometimes it is desirable and even necessary to reformat long lines of text or code into fewer columns than the original document or program contained. In such cases, it would be a great timesaver to use a computer program to do what might have otherwise been a manual task. This sample program is intended to be run on an ASCII platform to prepare program code for transfer to z/OS. To run it directly on z/OS, you must modify the FILE and INFILE statements in the code.
For example, suppose that you have a macro variable that contains 100 long variable names. Since the longest line of SAS program input is 256 columns, you have to break the line length to a more manageable size. For the sake of legibility, you want to restrict the number of variables per line in a SAS program to, say, 80 columns. The %TEXTWRAP MACRO has been written for this purpose. Instead of guessing how many variables the line will hold you can use the %TEXTWRAP MACRO to perform the reformatting operation. It reformats space-delimited text into a specified number of characters per line. In another example, you might use SAS as a program generator to create a long line of code that represents a complex computation. Given the input line size constraint of 256 columns, you have to decide how many terms to put on each line in your program. The %TEXTWRAP MACRO can help in this case, too.
Let us suppose that we have lines of code such as the ones below that must be recast into fewer columns.
/* Examples of text wrapping */ ShortExpression = b0 + b1*x1 + b2*x2 + b3*x3 ; LongExpression = b0 + b1*x1 + b2*x2 + b3*x3 + b4*x4 + b5*x5 + b6*x6 + b7*x7 + b8*x8 + b9*x9 + b10*x10 ;
We want to reformat the text into 60 columns. Let's say that it can be found in the file Expressions.txt. We invoke the MACRO with the following code:
%include 'y:\My SAS Files\9.1\Test\textWrap\textwrap.sas' ; %let IN = Y:\My SAS Files\9.1\Test\TextWrap\Expressions.txt ; %let OUT = Y:\My SAS Files\9.1\Test\TextWrap\Expressions textwrapped.txt ; %TEXTWRAP( &IN, &OUT, linelen=60 )
And we see the resulting wrapped text:
/* Example of text wrapping */ ShortExpression = b0 + b1*x1 + b2*x2 + b3*x3 ; LongExpression = b0 + b1*x1 + b2*x2 + b3*x3 + b4*x4 + b5*x5 + b6*x6 + b7*x7 + b8*x8 + b9*x9 + b10*x10 ;
We can use the %TEXTWRAP MACRO within a SAS data step, too, to overcome the line length constraint:
Data results ; Set inputData ; %TEXTWRAP( &IN, &OUT, linelen=60 ) Run ;
We can apply the %TEXTWRAP MACRO to text as easily as to code. Here is an abridged example, provided by Jan Squillace of SAS Technical Support. It is the first verse to the Shaker song, "Simple Gifts," followed by an excerpt from supplementary material.
'Tis the gift to be simple, 'tis the gift to be free, 'Tis the gift to come down where we ought to be, And when we find ourselves in the place just right, 'Twill be in the valley of love and delight. When true simplicity is gain'd To bow and to bend we shan't be asham'd, To turn, turn will be out delight 'Till by turning, turning we come round right. The SIMPLE GIFTS program is underpinned by four settings of the popular Shaker tune "'Tis the gift to be simple": the first drawn from a Shaker manuscript; the second in an arrangement by American musical icon Aaron Copland (1900-1990), who made the tune famous by including it in his ballet score Appalachian Spring … .
When this text is wrapped into 60 columns, we have the following reformatted result:
'Tis the gift to be simple, 'tis the gift to be free, 'Tis the gift to come down where we ought to be, And when we find ourselves in the place just right, 'Twill be in the valley of love and delight. When true simplicity is gain'd To bow and to bend we shan't be asham'd, To turn, turn will be out delight 'Till by turning, turning we come round right. The SIMPLE GIFTS program is underpinned by four settings of the popular Shaker tune "'Tis the gift to be simple": the first drawn from a Shaker manuscript; the second in an arrangement by American musical icon Aaron Copland (1900-1990), who made the tune famous by including it in his ballet score Appalachian Spring … .
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
The SAS code for the %TEXTWRAP MACRO is given below.
Remember that the text must be delimited by spaces to be parsed properly. If another delimiter is desired, the statement ndx = index( textout, ' ' ) ; must be modified or supplemented with a similar statement using the second delimiter.
/* textwrap.sas */
%macro TEXTWRAP( FILEIN /* name of input file containing text */
, FILEOUT /* name of output file to contain
reformatted text */
, LINELEN=80 /* [optional] maximum length of line of
reformatted text */
, RECFM=v /* [optional] output file record format */
) ;
/* PURPOSE: read text as stream from &FILEIN, reformat to max of
* &LINELEN characters
* write to &FILEOUT
*
* EXAMPLE OF USE:
* %let FILE_IN = c:\my SAS files\9.1\text_in ;
* %let FILE_OUT = c:\my SAS files\9.1\text_out ;
*
* %TEXTWRAP( &FILEIN, &FILEOUT, linelen=60 )
*/
data _null_ ;
length textin $ 32767 textout $ &LINELEN ;
/* to run this macro on z/OS, you must make changes to the
FILE and INFILE statements below
*/
file "&FILEOUT" lrecl=&LINELEN recfm=&RECFM ;
infile "&FILEIN" lrecl=32767 truncover;
format textin $char32767.;
input textin $char32767.;
if lengthn( textin ) <= &LINELEN
then put textin ;
else
do while( lengthn( textin ) > &LINELEN ) ;
textout = reverse( substr( textin, 1, &LINELEN )) ;
ndx = index( textout, ' ' ) ;
if ndx
then do ;
textout = reverse( substr( textout, ndx + 1 )) ;
put textout $char&linelen.. ;
textin = substr( textin, &LINELEN - ndx + 1 ) ;
end ;
else do;
textout = substr(textin,1,&LINELEN);
put textout $char&linelen.. ;
textin = substr(textin,&LINELEN+1);
end;
if lengthn( textin ) le &LINELEN then put textin $char&linelen.. ;
end ;
run ;
%mend TEXTWRAP ;
%textwrap(C:\SASPGM\SampleKeepers\longwordsleading.txt,c:\temp\longtxt1.txt,linelen=70);
run;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Topic: | SAS Reference ==> Macro |
Date Modified: | 2019-06-04 12:36:56 |
Date Created: | 2007-11-29 14:30:00 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | Microsoft Windows Server 2003 Enterprise Edition | ||
Microsoft Windows Server 2003 Datacenter Edition | ||||
Microsoft Windows NT Workstation | ||||
OpenVMS VAX | ||||
Microsoft® Windows® for 64-Bit Itanium-based Systems | ||||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | ||||
Microsoft Windows 2000 Professional | ||||
Microsoft Windows 2000 Server | ||||
Microsoft Windows 2000 Datacenter Server | ||||
Microsoft Windows 2000 Advanced Server | ||||
Microsoft Windows 95/98 | ||||
Windows | ||||
Microsoft® Windows® for x64 | ||||
Microsoft Windows XP 64-bit Edition | ||||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | ||||
z/OS | ||||
Tru64 UNIX | ||||
Solaris | ||||
Solaris for x64 | ||||
Linux on Itanium | ||||
OpenVMS Alpha | ||||
Linux | ||||
HP-UX IPF | ||||
IRIX | ||||
AIX | ||||
HP-UX | ||||
ABI+ for Intel Architecture | ||||
64-bit Enabled Solaris | ||||
64-bit Enabled AIX | ||||
64-bit Enabled HP-UX | ||||
Windows Vista | ||||
Windows Millennium Edition (Me) | ||||
WINDOWS/NTSV | ||||
Microsoft Windows Server 2003 Standard Edition | ||||
Microsoft Windows XP Professional |