Wrapit -- Wrap SAS input/SPSS data list statements into columns


wrapit.pl is a perl program which converts SAS input or SPSS data list statements, that are into one variable per line, into columns.

This assumes, as per SAS variable naming rules, all variable names begin with a letter.

It also assumes the method of input is columns. See the example in the source code below.

Five columns are assumed for the output. If you want more or less than that, change "$i+5" to the use number of columns you want instead of 5.

Source Code


#!/opt/gnu/bin/perl
# (set to the location of perl on your system)

# Wrapit: Read input or data list statements (e.g. "VAR1 12-18 .4", one
# line at a time and output them in wrapped format, five to a line
#  Kent Nassen, 4/22/97 & 8/15/97

# Sample input:
#  V1 1-4
#  V2 5-8
#  V3 9-9
#  V4 10-13
#  V5 14-14
#  V6 15-16
#  V7 17-17
#  V8 18-19
#  V9 20-21

# Sample output:
#   V1 1-4                   V2 5-8                   V3 9-9                   
#   V4 10-13                 V5 14-14                 V6 15-16                 
#   V7 17-17                 V8 18-19                 V9 20-21

# Read the input statements into an array
while (<>) {           # read from file on commandline
  chop;                # drop line ends
  for ($_) { s/^\s+//; s/\s+$//; } # strip leading & trailing blanks
  push @elements, $_;  # build array
  }

# Print out the array elements in preferred order & format
   for ($i=0; $i<=$#elements; $i+=5) {  # we want a 5-wide listing
      printf("   %-15s%-15s%-15s%-15s%-s\n",${elements}[$i],${elements}[$i+1],
		${elements}[$i+2],${elements}[$i+3],${elements}[$i+4]); 
   }


Back to Kent's Perl Page