Discussion:
Proc export - Customize decimal places
(too old to reply)
Hari
2006-01-29 08:22:24 UTC
Permalink
Hi,

Im going to write out SAS data in to txt format (delimiter is space)
and while doing that I would like to keep the decimal points for
different numeric variables as 2 only. Presently I have variables from
Var32 to Var56 and they have different widths (scales are different)
and different number of decimal places. I want to tamper around with
the number of decimal places only. Please suggest a way to do the same.

(Once am in front of SAS, I will try to see whether pasting from Log of
Proc export and changing the number of decimal places would do the
trick).

Regards,
Hari
India
tanwan
2006-01-30 03:56:19 UTC
Permalink
Hari,

the following code will write a space-delimited text file, formatted to
order /*check out tempted.txt to verify this*/, but obviously it ain't
no EXPORT procedure. Clearly you need to hard-code the column positions
"@" /* or you can automate that. I failed! */, but aside that, I guess
the end justified the means. data _null_ himself will likely agree.
GOOD LUCK!! (It doesn't come so often these days)

data aaa;
input Age Height Weight;
cards;
12 24.234543 39.0123
38 43.999967 88.7642
;
proc print data=aaa;run;
DATA _NULL_;
SET aaa;
FILE 'c:\tempted.txt';
PUT @1 Age 3.1 @6 Height 4.2 @12 Weight 10.4;
RUN;

/*just comapre the formating in the print and the text files*/
Hari
2006-01-30 03:58:57 UTC
Permalink
Hi,

When I look up the log for Proc Export for my above problem then I get
the following (I changed only the variable names and the number of
variables. Also , I dont want to write out the variable names in my
outfile so removed that loop. Othe than these changes I have not
tampered with the code below.)

data _null_;
set &FinalHBRegData end=EFIEOD;
%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
%let _EFIREC_ = 0; /* clear export record count macro variable
*/
file '&ProjectRootFolderPath.\Working\Exported files\2006 01
30\&FinalHBRegData..csv'
delimiter=',' DSD DROPOVER lrecl=32767;
format InterCeptDumm best12. ;
format IndepVar1 best12. ;
format IndepVar2 best12. ;
format IndepVar3 best12. ;
format IndepVar4 best12. ;
format DepenVar best12. ;
do;
EFIOUT + 1;
put InterCeptDumm @;
put IndepVar1 @;
put IndepVar2 @;
put IndepVar3 @;
put IndepVar4 @;
put DepenVar @;
;
end;
if _ERROR_ then call symputx('_EFIERR_',1); /* set ERROR detection
macro variable */
if EFIEOD then call symputx('_EFIREC_',EFIOUT);
run;


I want to store the write-out format for my numeric variables
(IndepVar1 to DepenVar with number of decimal places as 2. I dont know
how to control that. The widths of different numeric variables are
quite different for example, InterceptDumm (Stands for value of
Intercept which is always 1) has width as 1 and the IndepVar1/IndepVar2
has maximum of value 9999 and number of decimal places varying from one
row to another (as seen from data file view). Similarly other 3
variables IndepVar3, IndepVar4 and DepenVar hav maximum values as lets
say a six digit number. In short how do I customize my above variables
to have

Also, the PUT statement in Log of Proc Export shows that it is writing
out my number kind of variables as String (@) Why is this happening? I
can understand such kind of problems happening in case of Proc Import
but why should it happen while exporting?

Just to add am facing one more problem. When I try to read in numerical
data from output of another software then SAS is reading it as String
(My first 20 rows of that variable is missing). K , I can probably
overcome this by using the Log of Proc Import, but am concerned that
even when I dont have string data in any of my variables (string used
to get truncated while using Proc Import), then am forced to use the
Log of proc Import.

Regards,
Hari
India
Peter Crawford
2006-01-30 15:38:24 UTC
Permalink
Post by Hari
Hi,
When I look up the log for Proc Export for my above problem then I get
the following (I changed only the variable names and the number of
variables. Also , I dont want to write out the variable names in my
outfile so removed that loop. Othe than these changes I have not
tampered with the code below.)
data _null_;
set &FinalHBRegData end=EFIEOD;
%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
%let _EFIREC_ = 0; /* clear export record count macro variable
*/
file '&ProjectRootFolderPath.\Working\Exported files\2006 01
30\&FinalHBRegData..csv'
delimiter=',' DSD DROPOVER lrecl=32767;
format InterCeptDumm best12. ;
format IndepVar1 best12. ;
format IndepVar2 best12. ;
format IndepVar3 best12. ;
format IndepVar4 best12. ;
format DepenVar best12. ;
do;
EFIOUT + 1;
;
end;
if _ERROR_ then call symputx('_EFIERR_',1); /* set ERROR detection
macro variable */
if EFIEOD then call symputx('_EFIREC_',EFIOUT);
run;
I want to store the write-out format for my numeric variables
(IndepVar1 to DepenVar with number of decimal places as 2. I dont know
how to control that. The widths of different numeric variables are
quite different for example, InterceptDumm (Stands for value of
Intercept which is always 1) has width as 1 and the IndepVar1/IndepVar2
has maximum of value 9999 and number of decimal places varying from one
row to another (as seen from data file view). Similarly other 3
variables IndepVar3, IndepVar4 and DepenVar hav maximum values as lets
say a six digit number. In short how do I customize my above variables
to have
Also, the PUT statement in Log of Proc Export shows that it is writing
can understand such kind of problems happening in case of Proc Import
but why should it happen while exporting?
Just to add am facing one more problem. When I try to read in numerical
data from output of another software then SAS is reading it as String
(My first 20 rows of that variable is missing). K , I can probably
overcome this by using the Log of Proc Import, but am concerned that
even when I dont have string data in any of my variables (string used
to get truncated while using Proc Import), then am forced to use the
Log of proc Import.
Regards,
Hari
India
Use proc import when you know little about the data.
When you know the structure (data types), just use a
DATA step. It is really simple for anything that a basic
proc import might be able to handle.

data <output table name> ;
length /* define all columns on the input, whether
numeric or character, with their (output) lengths,
and in the order of the input columns */
;
informat /*define columns needing informats, like yymmdd10.
;
infile /* define input file, including
lrecl= if >256
DSD when handling delimited data
dlm= if delimiter is not comma */
;
input <firstvariable> -- <lastvariable> ;
run;

Reading a csv file should be that simple.

When writing out data, it may be effective to adopt a similar
policy when anything other than default style is needed.

data _null_ ;
file /*define the required output data file with
DSD for delimited text output
lrecl= when >256
dlm= provide the delimiter when other than comma
***********************/
;
put 'column,header,as,you,choose' ;
/*can be more complex, but ... why ? */
do until( end_of_input );
set /* define the input data set(s) */
end= end_of_input ;
;
/* where subsetting if neccessary ; ***/
put ( list of columns to extract )(:) ;
end;
/*
put a trailer record to indicate end-of-file if required*/
stop ;
run;


Good Luck

Peter Crawford
Howard Schreier <hs AT dc-sug DOT org>
2006-01-30 23:11:57 UTC
Permalink
Post by Hari
Hi,
When I look up the log for Proc Export for my above problem then I get
the following (I changed only the variable names and the number of
variables. Also , I dont want to write out the variable names in my
outfile so removed that loop. Othe than these changes I have not
tampered with the code below.)
data _null_;
set &FinalHBRegData end=EFIEOD;
%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
%let _EFIREC_ = 0; /* clear export record count macro variable
*/
file '&ProjectRootFolderPath.\Working\Exported files\2006 01
30\&FinalHBRegData..csv'
delimiter=',' DSD DROPOVER lrecl=32767;
format InterCeptDumm best12. ;
format IndepVar1 best12. ;
format IndepVar2 best12. ;
format IndepVar3 best12. ;
format IndepVar4 best12. ;
format DepenVar best12. ;
do;
EFIOUT + 1;
;
end;
if _ERROR_ then call symputx('_EFIERR_',1); /* set ERROR detection
macro variable */
if EFIEOD then call symputx('_EFIREC_',EFIOUT);
run;
I want to store the write-out format for my numeric variables
(IndepVar1 to DepenVar with number of decimal places as 2. I dont know
how to control that. The widths of different numeric variables are
quite different for example, InterceptDumm (Stands for value of
Intercept which is always 1) has width as 1 and the IndepVar1/IndepVar2
has maximum of value 9999 and number of decimal places varying from one
row to another (as seen from data file view). Similarly other 3
variables IndepVar3, IndepVar4 and DepenVar hav maximum values as lets
say a six digit number. In short how do I customize my above variables
to have
This seems pretty straightforward; just use the appropriate w.d format for
each variable. If I understand, d will always be 2.
Post by Hari
Also, the PUT statement in Log of Proc Export shows that it is writing
can understand such kind of problems happening in case of Proc Import
but why should it happen while exporting?
The at (@) sign tells SAS to hold the buffer so that it can write some more
on the same line. It is not bound to any preceding variable name and has
nothing to do with data types (numeric vs. character). Maybe you are
confusing it with the dollar($) sign.
Post by Hari
Just to add am facing one more problem. When I try to read in numerical
data from output of another software then SAS is reading it as String
(My first 20 rows of that variable is missing). K , I can probably
overcome this by using the Log of Proc Import, but am concerned that
even when I dont have string data in any of my variables (string used
to get truncated while using Proc Import), then am forced to use the
Log of proc Import.
You are using a DATA step, and DATA steps can be crafted to do just about
anything in this vein. The fact that you are developing your DATA step by
tweaking one generated by the PROC, instead of coding from scratch, is very
much secondary.

Problems with PROC IMPORT or the Import Wizard are never-ending on this
list. If those things work for you, you are lucky. Otherwise leave them
behind and be glad that SAS has more versatile tools.
Post by Hari
Regards,
Hari
India
Hari
2006-01-31 06:47:58 UTC
Permalink
Howard,

Thanks for your post.
Maybe you are confusing it with the dollar($) sign.
This is what happens when one doesnt read about the basics!! Just to
add I partly got "muddled up" because while reading the output using
proc import numbers were being read as strings, So I thought that this
is probably also happeneing with Proc Export. Im sorry for the
confusion.
This seems pretty straightforward; just use the appropriate w.d format for each variable. If I understand, d will always be 2.
The issue I have is, I dont know as to what the width of my various
variables will be (This is in contrary to what I stated in my first
post, but probablyl I couldnt express my request properly). Its like
some independent variables can be of maximum digits being 5 and in
another data file of similar structure the same variable can be having
maximum width of lets say 7. So what do I write in for "w" within the
"w.d"?

Regards,
Hari
India
David L Cassell
2006-01-31 07:46:45 UTC
Permalink
***@YAHOO.COM wrote back:
hs> > Maybe you are confusing it with the dollar($) sign.
Post by Hari
This is what happens when one doesnt read about the basics!! Just to
add I partly got "muddled up" because while reading the output using
proc import numbers were being read as strings, So I thought that this
is probably also happeneing with Proc Export. Im sorry for the
confusion.
hs> >This seems pretty straightforward; just use the appropriate w.d format
for each variable. If I understand, d will always be 2.
Post by Hari
The issue I have is, I dont know as to what the width of my various
variables will be (This is in contrary to what I stated in my first
post, but probablyl I couldnt express my request properly). Its like
some independent variables can be of maximum digits being 5 and in
another data file of similar structure the same variable can be having
maximum width of lets say 7. So what do I write in for "w" within the
"w.d"?
Just pick a big enough number for *everything*. Since your original PROC
EXPORT log indicated that a BEST12. format would work, I suspect that
you could use the 12.2 format. If your number is less than 12 digits, no
sweat. Try it out and see how it works.

Similarly, there's a huge amount of code generated by PROC EXPORT that you
just won't need, except under really unusual circumstances. Ditch most of
it,
and just keep the parts that do what you want. Your code will be lighter,
and you'll feel better about it.

HTH,
David
--
David L. Cassell
mathematical statistician
Design Pathways
3115 NW Norwood Pl.
Corvallis OR 97330

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar – get it now!
http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/
Howard Schreier <hs AT dc-sug DOT org>
2006-01-31 21:40:33 UTC
Permalink
If you really must make the fields no wider than necessary while assuring
that 2 places appear to the right of the decimal, you will have to make 2
passes. The first will use PROC SUMMARY or PROC SQL or something to find
the largest value for each variable, and possibly the negative value of
greatest magnitude. From that information, you can derive the minimal "w"
for each variable.
Post by David L Cassell
hs> > Maybe you are confusing it with the dollar($) sign.
Post by Hari
This is what happens when one doesnt read about the basics!! Just to
add I partly got "muddled up" because while reading the output using
proc import numbers were being read as strings, So I thought that this
is probably also happeneing with Proc Export. Im sorry for the
confusion.
hs> >This seems pretty straightforward; just use the appropriate w.d format
for each variable. If I understand, d will always be 2.
Post by Hari
The issue I have is, I dont know as to what the width of my various
variables will be (This is in contrary to what I stated in my first
post, but probablyl I couldnt express my request properly). Its like
some independent variables can be of maximum digits being 5 and in
another data file of similar structure the same variable can be having
maximum width of lets say 7. So what do I write in for "w" within the
"w.d"?
Just pick a big enough number for *everything*. Since your original PROC
EXPORT log indicated that a BEST12. format would work, I suspect that
you could use the 12.2 format. If your number is less than 12 digits, no
sweat. Try it out and see how it works.
Similarly, there's a huge amount of code generated by PROC EXPORT that you
just won't need, except under really unusual circumstances. Ditch most of
it,
and just keep the parts that do what you want. Your code will be lighter,
and you'll feel better about it.
HTH,
David
--
David L. Cassell
mathematical statistician
Design Pathways
3115 NW Norwood Pl.
Corvallis OR 97330
_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar – get it now!
http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/
Loading...