Date: Sat, 2 Apr 2005 20:55:14 +0000
Reply-To: iw1junk@COMCAST.NET
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ian Whitlock <iw1junk@COMCAST.NET>
Subject: Re: flat file from proc contents?
David,
Let's put together a pretty simple example. Suppose you want
make flat files according to the rules:
if the variable is character then write out the full length of
the variable.
if the variable is numeric stored in 3 (or less) bytes then
assume precise integer and use format 4. (largest value 8192
on a PC or UNIX) otherwise use format BEST16.
Here is some data.
data w ;
length x $ 25 y 3 ;
x = "abc" ; y = 7 ; z = 4.3 ; output ;
x = "abcooooooooooooooooooend!" ; y = 7000 ;
z = 44444444444.33333 ; output ;
run ;
Here is the desired program.
filename out temp ;
data _null_ ;
file out ;
set w ;
put x $char25.
y 4.
z best16.
;
run ;
We could get the specs by
proc contents out = c noprint ;
run ;
proc print data = c ;
var name type length ;
run ;
and reading
Obs NAME TYPE LENGTH
1 x 2 25
2 y 1 3
3 z 1 8
Now if the data set changes we have to do the work all over. But
wait the instructions are so simple, why can't a DATA step read
the contents and set up the input statement? Here is the DATA
step.
data _null_ ;
length spec $ 32767 fmt $ 13 ;
retain spec ;
if eof then call symput ( "spec" , trim(spec) ) ;
set c end = eof ;
if type = 2 then
fmt = "$char" || trim(put (length, 5. -l)) || "." ;
else
if length = 3 then
fmt = "4." ;
else
fmt = "best16." ;
spec = trimn(spec) || " " || name || fmt ;
run ;
Now the following simple step produces the file.
filename out temp ;
data _null_ ;
file out lrecl=32767 ;
set w ;
put &spec ;
run ;
Here is a step to check the result.
data _null_ ;
infile out obs = 10 ;
input ;
list ;
run ;
It produces
NOTE: The infile OUT is:
File Name=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\SAS Temporary
Files\_TD1108\#LN00005,
RECFM=V,LRECL=256
RULE:
----+----1----+----2----+----3----+----4----+----5----+----6----+
1 abc 7 4.3 45
2 abcooooooooooooooooooend!700044444444444.3333 45
NOTE: 2 records were read from the infile OUT.
The minimum record length was 45.
The maximum record length was 45.
You probably don't want just a temporary file and you might want
the code to work with data sets having names other than W. So
put the code in a macro add parameters DATA for the data set name
and FILE for the fileref and you are all set with a finished
example.
Since the same information can be found in the SQL file
DICTIONARY.COLUMNS you are likely to find variations using this
file with SQL code for the DATA step creating SPEC, or with
SASHELP.VCOLUMN, which is a view of DICTIONARY.COLUMNS, instead
of the older PROC CONTENTS.
Once you understand this example you should be able to make up a
problem of interest to you that can be solved with information
found in PROC CONTENTS.
In SUGI 19 (1994) I wrote a paper expanding on this idea to
obtain a very flexible flat file maker with a self generated
layout. In SUGI 11, 1986, I co-authored a paper with my wife
giving a number of examples using PROC CONTENTS to solve various
problems. Half the paper detailed how to write the contents to a
file and then read it because there was no OUT= option in SAS
82.4.
Neither of these papers is electronically available. However,
using the search facility at http://www.lexjansen.com
and looking for "flat file proc contents" I got 39 hits, some of
which probably present variations on this theme. The words
"external file dictionary.columns" produced 6 hits, and "external
file sashelp.vcolumn" produced 3 more.
Ian Whitlock
==================
Date: Fri, 1 Apr 2005 17:56:07 -0800
Reply-To: David Fickbohm <davefickbohm@YAHOO.COM>
Sender: "SAS(r) Discussion"
Comments: DomainKeys? See
http://antispam.yahoo.com/domainkeys
From: David Fickbohm <davefickbohm@YAHOO.COM>
Subject: flat file from proc contents?
Content-Type: text/plain; charset=us-ascii
People,
I read a paper about proc datasets. In the paper the author said
there are lots of papers on how to use the output of the contents
to create a flat file. Can someone point me at a paper that
explains how to do that please?
Thanks
Dave
Dave Fickbohm
Use Technology to the Fullest
1250 45th st suite 200
Emeryville, CA, 94608
510 594 4151 voice
|