Sample 24672: Splitting long comment variables on word boundaries
Split a variable containing a very long comment into shorter variables without splitting a word.
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.
/* Download sample data from the DOWNLOADS tab. Edit the libname */
/* statement to point to the location of the downloaded data set. */
/* */
/* For use with other data, edit the %LET statements as needed. */
/* This sample passes 1000 as the length of the original string */
/* to be split into new strings of 100 bytes each. */
libname foo 'c:\';
%let ol=1000; /* length of original string */
%let nw=100; /* length of new, smaller string */
%let ad=11; /* number of new strings to create (ol/nw)+1 */
options ls=110;
data new;
/* For illustration purposes only */
if _n_=1 then do;
r='----+----0';
rule=repeat(r,9);
put rule;
end;
keep comment:;
set foo.fusion_24672_1_test (rename=(comments=orig));
array comment(&ad) $ &nw;
length bin $ &ol;
/* Determine if current COMMENT needs to be split into smaller pieces */
num=round(length(orig)/100)+1;
/* If num is 1, no manipulation is required, just reassign and output */
if num=1 then do;
comment1=orig;
put comment1;
end;
/* Else, break into new strings, shorter than the max width specified */
else do;
/* Create holding bin of 'what's left to be parsed' when a chunk is removed */
bin=orig;
do j=1 to num;
/* Parse to break on a word boundary */
do i=100 to 0 by -1;
orig=bin;
/* When you find a word boundary, put the first half in the array */
/* variable,output, and the rest in the bin for further processing. */
if substr(orig,i,1)=' ' then do;
comment(j)=substr(bin,1,i);
put comment(j);
/* Assign rest back into BIN, starting one byte after the location of the space */
bin=substr(bin,i+1);
/* Dont need to search for a space anymore, so leave this inner do loop */
leave;
end;
end;
end;
end;
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.
OUTPUT to SAS log
----+----0----+----0----+----0----+----0----+----0----+----0----+----0----+----0----+----0----+----0
An array is a temporary grouping of SAS variables that are arranged in a particular order and
identified by an array-name. The array exists only for the duration of the current DATA step. The
array-name distinguishes it from any other arrays in the same DATA step; it is not a variable.
A multidimensional array is a more complex grouping of variables that, when processed, results in
output that could have two or more dimensions, such as columns and rows.
A SAS data library is a collection of one or more SAS files that are recognized by SAS and that are
referenced and stored as a unit. Each file is a member of the library. The logical concept of a
SAS data library remains constant, regardless of the operating environment. In any operating
environment where SAS can be installed, the structure for organizing,locating, and managing SAS
files is the same. At the operating environment level,however, a SAS data library has different
physical implementations. Most SAS data libraries implement the storage of files in a manner
similar to the way the operating environment stores and accesses files. For instance, in
directory-based operating environments, a SAS data library is a group of SAS files that are stored
in the same directory and accessed by the same engine. Other files can be stored in the directory,
but only the files with file extensions that are assigned by SAS are recognized as part of the SAS
data library.
Split a variable containing a very long comment into
shorter variables without splitting a word.
Type: | Sample |
Topic: | Data Management ==> Manipulation and Transformation ==> Array processing Common Programming Tasks ==> Utilities SAS Reference ==> Statements ==> Information ==> ARRAY
|
Date Modified: | 2008-02-08 16:15:30 |
Date Created: | 2004-09-30 14:09:02 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |