<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Keep blanks when exporting to .txt in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Keep-blanks-when-exporting-to-txt/m-p/336230#M22350</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(I'm using SAS Enterprise Guide 7.1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to make a txt-file that can be read by a program on my computer.&lt;/P&gt;&lt;P&gt;In order for the program to read the file, all values need to be placed on an exact position within the file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset consisting of a number of character variables, some that have leading blanks and some that have trailing blanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table name as
select
'BI' as inter,
'4F' as type,
'25300   ' as account,
put(round(compress(sum(payment,tax),0.01)*100,z19.)) as amount,
put(intnx('Month' , &amp;amp;now. , 0 , 's'),YYMMDDN8.) as trans_date
from source;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When exporting the dataset to txt-file I need the length of the variables to remain the same, i.e. the txt-file needs to include leading and trailing blanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried using proc export, but the blanks are removed.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc export data=name outfile="c:\name.txt" dbms=dlm replace;
putnames=NO;
delimiter='';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea on how to do that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 27 Feb 2017 15:18:56 GMT</pubDate>
    <dc:creator>cat2</dc:creator>
    <dc:date>2017-02-27T15:18:56Z</dc:date>
    <item>
      <title>Keep blanks when exporting to .txt</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Keep-blanks-when-exporting-to-txt/m-p/336230#M22350</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(I'm using SAS Enterprise Guide 7.1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to make a txt-file that can be read by a program on my computer.&lt;/P&gt;&lt;P&gt;In order for the program to read the file, all values need to be placed on an exact position within the file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset consisting of a number of character variables, some that have leading blanks and some that have trailing blanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table name as
select
'BI' as inter,
'4F' as type,
'25300   ' as account,
put(round(compress(sum(payment,tax),0.01)*100,z19.)) as amount,
put(intnx('Month' , &amp;amp;now. , 0 , 's'),YYMMDDN8.) as trans_date
from source;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When exporting the dataset to txt-file I need the length of the variables to remain the same, i.e. the txt-file needs to include leading and trailing blanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried using proc export, but the blanks are removed.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc export data=name outfile="c:\name.txt" dbms=dlm replace;
putnames=NO;
delimiter='';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea on how to do that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Feb 2017 15:18:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Keep-blanks-when-exporting-to-txt/m-p/336230#M22350</guid>
      <dc:creator>cat2</dc:creator>
      <dc:date>2017-02-27T15:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: Keep blanks when exporting to .txt</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Keep-blanks-when-exporting-to-txt/m-p/336236#M22351</link>
      <description>&lt;P&gt;Use datastep and put:&lt;/P&gt;
&lt;PRE&gt;data _null_;
  file "afile.txt";
  put var1 $10. var2 $20.;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Feb 2017 15:25:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Keep-blanks-when-exporting-to-txt/m-p/336236#M22351</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-02-27T15:25:56Z</dc:date>
    </item>
    <item>
      <title>Re: Keep blanks when exporting to .txt</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Keep-blanks-when-exporting-to-txt/m-p/336252#M22352</link>
      <description>&lt;P&gt;Use a data step to write the text file. &amp;nbsp;Note that if the data needs to be in fixed locations then you do not want a delimited file.&lt;/P&gt;
&lt;P&gt;In general you can use a format to fix how many columns a value takes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your example translates into this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  file "c:\name.txt" ;
  set source ;
  amount = round(100*sum(payment,tax),1);
  trans_date = intnx('Month' , &amp;amp;now. , 0 , 's') ;
  put 'BI' '4F' '25300   ' amount Z19. trans_date yymmddn8. ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do have character variables with leading spaces you can use the $CHAR format to make sure that SAS preserves the leading spaces. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your format just has blank columns you can either include string literals in your PUT statement or use cursor movement such as +1 or&amp;nbsp;@12 to move to where on the line you want to write the next value. &amp;nbsp;For example if ACCOUNT was actually coming as a variable and you always wanted the 19 digits of AMOUNT to appear starting in column 13 your put statement might look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  put 'BI' '4F' account @13 amount Z19. trans_date yymmddn8. ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS. Note that SAS cannot actually store 19 distinct digits in a single floating point number. The mazimum precision is really only about 15 digits. &amp;nbsp;So if your amounts are too large the last few digits could be lost.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Feb 2017 16:13:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Keep-blanks-when-exporting-to-txt/m-p/336252#M22352</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-02-27T16:13:35Z</dc:date>
    </item>
    <item>
      <title>Re: Keep blanks when exporting to .txt</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Keep-blanks-when-exporting-to-txt/m-p/336264#M22353</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;May not exactly solve your problem, but you may be able to enhance it.

Ian Whitlock - Create flatfile with layout

HAVE (sashelp.class)
====================

Up to 40 obs from SASHELP.CLASS total obs=19

Obs    NAME       SEX    AGE    HEIGHT    WEIGHT

  1    Alfred      M      14     69.0      112.5
  2    Alice       F      13     56.5       84.0
  3    Barbara     F      13     65.3       98.0
  4    Carol       F      14     62.8      102.5
  5    Henry       M      14     63.5      102.5
  6    James       M      12     57.3       83.0
  7    Jane        F      12     59.8       84.5
  8    Janet       F      15     62.5      112.5
  9    Jeffrey     M      13     62.5       84.0
 10    John        M      12     59.0       99.5
 11    Joyce       F      11     51.3       50.5
 12    Judy        F      14     64.3       90.0
 13    Louise      F      12     56.3       77.0
 14    Mary        F      15     66.5      112.0
 15    Philip      M      16     72.0      150.0
 16    Robert      M      12     64.8      128.0
 17    Ronald      M      15     67.0      133.0
 18    Thomas      M      11     57.5       85.0
 19    William     M      15     66.5      112.0


WANT  flatfile with layout)
===========================

Layout for SASHELP.CLASS to d:/txt/class.txt

              Column
Column Name   Type    fmtlen
---------------------------------
NAME          char    $8. +1
SEX           char    $1. +1
AGE           num     best10. +1
HEIGHT        num     best10. +1
WEIGHT        num     best10. +1

d:/txt/class.txt

----|----10---|----20---|----30---|----40---|

Alfred   M         14         69      112.5
Alice    F         13       56.5         84
Barbara  F         13       65.3         98
Carol    F         14       62.8      102.5
Henry    M         14       63.5      102.5
James    M         12       57.3         83
Jane     F         12       59.8       84.5
Janet    F         15       62.5      112.5
Jeffrey  M         13       62.5         84
John     M         12         59       99.5
Joyce    F         11       51.3       50.5
Judy     F         14       64.3         90
Louise   F         12       56.3         77
Mary     F         15       66.5        112
Philip   M         16         72        150
Robert   M         12       64.8        128
Ronald   M         15         67        133
Thomas   M         11       57.5         85
William  M         15       66.5        112

FULL SOLUTION
=============


* Ian Whitlock;

%macro utl_flatfile(lib=,  /* libref of input sas dataset to convert */
   sasdsn=,                /* second-level sas dataset name */
   file=print,             /* name of file to write to  */
   tl=3,                   /* default is to generate a title3 stmt  */
   spaces=1                /* number of spaces separating output fields */
   );
   %local putlist ;
   %let lib=%upcase(&amp;amp;lib);
   %let sasdsn=%upcase(&amp;amp;sasdsn);

   proc sql noprint ;
      select
        case
          when format ^= " " then
               name || " " || format
          when upcase(type) = "CHAR" then
               name || " $char"||trim(left(put(length,3.)))||"."
          else
               name || " best10."
        end  into :putlist separated by " +&amp;amp;spaces "
        from dictionary.columns
        where libname = "&amp;amp;lib" and memname = "&amp;amp;sasdsn"
      ;
      reset print ;
      title&amp;amp;tl "Layout for &amp;amp;lib..&amp;amp;sasdsn to &amp;amp;file" ;
      select name , type ,
        case
          when format ^= " " then trim(format)
          when upcase(type) = "CHAR" then "$"||trim(left(put(length,3.)))||"."
          else "best10."
        end  || " +&amp;amp;spaces " as fmtlen
        from dictionary.columns
        where libname = "&amp;amp;lib" and memname = "&amp;amp;sasdsn"
      ;
      title&amp;amp;tl ;
   quit;

   data _null_;
     set &amp;amp;lib..&amp;amp;sasdsn;
     file "&amp;amp;file" ;
     put &amp;amp;putlist ;
   run;
%mend utl_flatfile;


%utl_flatfile(lib=sashelp,sasdsn=class,file=d:/txt/class.txt);


* check;
data cls;
  infile "d:/txt/class.txt";

  input
     name           $8. +1
     sex            $1. +1
     age            best10. +1
     height         best10. +1
     weight         best10. +1
  ;
run;quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Feb 2017 17:06:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Keep-blanks-when-exporting-to-txt/m-p/336264#M22353</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-02-27T17:06:59Z</dc:date>
    </item>
    <item>
      <title>Re: Keep blanks when exporting to .txt</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Keep-blanks-when-exporting-to-txt/m-p/336465#M22366</link>
      <description>&lt;P&gt;Thank you! So easy! Solved my problem perfectly!&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2017 08:22:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Keep-blanks-when-exporting-to-txt/m-p/336465#M22366</guid>
      <dc:creator>cat2</dc:creator>
      <dc:date>2017-02-28T08:22:25Z</dc:date>
    </item>
  </channel>
</rss>

