<?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 Re: output fixed length and record the position and length of a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424535#M104544</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114"&gt;@ciro&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure if that will answer all your requirements but did you have a look at&amp;nbsp;&lt;SPAN class="cs8F4F6A3F"&gt;&lt;STRONG&gt;&lt;EM&gt;%FLATFILE, and Make Your Life Easier&lt;/EM&gt;&lt;/STRONG&gt;, available from&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs8A70F0C8"&gt;&amp;nbsp; &lt;A class="cs13BC40F8" href="ftp://ftp.sas.com/techsup/download/misc/flatfile.pdf" target="new"&gt;&lt;SPAN class="cs896CB680"&gt;ftp://ftp.sas.com/techsup/download/misc/flatfile.pdf&lt;/SPAN&gt;&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs8A70F0C8"&gt;&lt;SPAN class="cs896CB680"&gt;Export of sashelp.cars looks like this:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs8A70F0C8"&gt;&lt;SPAN class="cs896CB680"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="export.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/17606i01F933AC014114B8/image-size/large?v=v2&amp;amp;px=999" role="button" title="export.png" alt="export.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs8A70F0C8"&gt;&lt;SPAN class="cs896CB680"&gt;Hope that helps.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs8A70F0C8"&gt;&lt;SPAN class="cs896CB680"&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;Damo&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 03 Jan 2018 10:40:42 GMT</pubDate>
    <dc:creator>Damo</dc:creator>
    <dc:date>2018-01-03T10:40:42Z</dc:date>
    <item>
      <title>output fixed length and record the position and length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424530#M104541</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;happy new year to everyone.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have to output a dataset of 100 and more variables&amp;nbsp;to a text file with a fixed length format. Each&amp;nbsp;field should be separated from the previous one with, at least, one&amp;nbsp;blank.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Moreover in another dataset I have to write for each variable at which position it starts and which is the (maximum) length of the field.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to do it programmatically.&lt;/P&gt;
&lt;P&gt;I imagine that I have to use the output of a proc contents and a, data-driven, data _null_ step, but I am not very good with it.&lt;/P&gt;
&lt;P&gt;Any help is appreciated.&lt;/P&gt;
&lt;P&gt;Thank you very much in advance&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jan 2018 10:23:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424530#M104541</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2018-01-03T10:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: output fixed length and record the position and length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424535#M104544</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114"&gt;@ciro&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure if that will answer all your requirements but did you have a look at&amp;nbsp;&lt;SPAN class="cs8F4F6A3F"&gt;&lt;STRONG&gt;&lt;EM&gt;%FLATFILE, and Make Your Life Easier&lt;/EM&gt;&lt;/STRONG&gt;, available from&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs8A70F0C8"&gt;&amp;nbsp; &lt;A class="cs13BC40F8" href="ftp://ftp.sas.com/techsup/download/misc/flatfile.pdf" target="new"&gt;&lt;SPAN class="cs896CB680"&gt;ftp://ftp.sas.com/techsup/download/misc/flatfile.pdf&lt;/SPAN&gt;&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs8A70F0C8"&gt;&lt;SPAN class="cs896CB680"&gt;Export of sashelp.cars looks like this:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs8A70F0C8"&gt;&lt;SPAN class="cs896CB680"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="export.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/17606i01F933AC014114B8/image-size/large?v=v2&amp;amp;px=999" role="button" title="export.png" alt="export.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs8A70F0C8"&gt;&lt;SPAN class="cs896CB680"&gt;Hope that helps.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class="cs95E872D0"&gt;&lt;SPAN class="cs8A70F0C8"&gt;&lt;SPAN class="cs896CB680"&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;Damo&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jan 2018 10:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424535#M104544</guid>
      <dc:creator>Damo</dc:creator>
      <dc:date>2018-01-03T10:40:42Z</dc:date>
    </item>
    <item>
      <title>Re: output fixed length and record the position and length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424545#M104550</link>
      <description>&lt;P&gt;It should be quite simple.&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set data.vcolumns (where=(libname="WORK" and memname="YOURDATA")) end=last;
  if _n_=1 then call execute('data _null_; file "yourfile.txt"; set work.yourdata; put ');
  call execute(cat(' ',name,' ',cats(type,length,'. " "'));
  if last then call execute(';run;');
run;&lt;/PRE&gt;
&lt;P&gt;Note, not tested&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jan 2018 11:13:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424545#M104550</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-01-03T11:13:30Z</dc:date>
    </item>
    <item>
      <title>Re: output fixed length and record the position and length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424553#M104553</link>
      <description>&lt;P&gt;Your code gives the following error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;num8. " "&lt;BR /&gt; -----&lt;BR /&gt; 48&lt;BR /&gt;ERROR 48-59: The format NUM was not found or could not be loaded.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I substituted as in the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;a='abc'; b=1.23; c='xyzy'; output;&lt;BR /&gt;a='ac'; b=11.23; c='xy'; output;&lt;BR /&gt;a='ab'; b=111.23; c='x'; output;&lt;BR /&gt;a='a'; b=1.2; c='zyz'; output;&lt;BR /&gt;a='a'; b=1; c=' '; output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt; set sashelp.vcolumn (where=(libname="WORK" and memname="HAVE")) end=last;&lt;BR /&gt; if _n_=1 then call execute('data _null_; file "g:\temp\test.txt"; set work.have; put ');&lt;BR /&gt; if type='num' then call execute(cat(' ',name,' ',cats('best',length,'. " "')));&lt;BR /&gt; else call execute(cat(' ',name,' ',cats(type,length,'. " "')));&lt;BR /&gt; if last then call execute(';run;');&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;however the result is not what I hoped.&lt;/P&gt;
&lt;P&gt;1. the numeric column is not left aligned&lt;/P&gt;
&lt;P&gt;2. I would like to know the position and the length of the field for each variable (as I have to document the data file)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jan 2018 12:32:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424553#M104553</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2018-01-03T12:32:09Z</dc:date>
    </item>
    <item>
      <title>Re: output fixed length and record the position and length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424563#M104558</link>
      <description>&lt;P&gt;Minor change then:&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set data.vcolumns (where=(libname="WORK" and memname="YOURDATA")) end=last;
  if _n_=1 then call execute('data _null_; file "yourfile.txt"; set work.yourdata; put ');
  call execute(cat(' ',name,' ',cats(tranwrd(type,"num",""),length,'. " "'));
  if last then call execute(';run;');
run;&lt;/PRE&gt;
&lt;P&gt;For this: "&lt;SPAN&gt;1. the numeric column is not left aligned", yes it is, a numeric is a combination of length and decimals, if length is 8 and number is 12 then the output string is "&amp;nbsp; &amp;nbsp; &amp;nbsp; 12".&amp;nbsp; So you would need to strip(put()) the value into a character string with preceeding spaces trimmed off.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2. I would like to know the position and the length of the field for each variable (as I have to document the data file)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It sounds like you are doing this all back to front.&amp;nbsp; If you are - correctly so - documenting a transfer, the documentation should be filled in beforehand as the agreement between two parties.&amp;nbsp; That is then signed off and the sender develops to write program, and receiver the read program using the agreement as the basis.&amp;nbsp; So you should have the spec before you start coding.&amp;nbsp; Otherwise a spec is pretty much useless to the end user.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;As a note, you will find a more standard format such as CSV or XML far simpler for all parties.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jan 2018 14:05:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424563#M104558</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-01-03T14:05:18Z</dc:date>
    </item>
    <item>
      <title>Re: output fixed length and record the position and length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424571#M104561</link>
      <description>&lt;P&gt;The following code seems to solve the issue, i really hope that someone can optimize (reduce amount of code) it.&lt;/P&gt;
&lt;P&gt;I assume that every numeric variable is formatted. Using the length of variables to determine the starting position is not good idea, because as data changes the programs using the exported dataset will have to be changed to - this can, of course, be automated by using the metadata created during the export. &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; already said: using csv will make everything easier on the long run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro WriteDatasetToFile(
   SourceDataset=,
   TargetFile=
);

   /* Get the variables of &amp;amp;SourceDataset */
   %local varList;

   proc sql noprint;
      select Name
         into :varList separated by ' '
         from sashelp.vcolumn
            where catx('.', LibName, MemName) = "%upcase(&amp;amp;SourceDataset.)"
      ;
   quit;

   /* Number of variables in &amp;amp;SourceDataset */
   %local varCount lineLength;
   %let varCount = %sysfunc(countw(&amp;amp;varList, %str( )));

   /* Get starting position and max length of each variable. */
   data work.meta;
      set work.class end= jobDone;

      length 
         VarName $ 32
         MaxLength 8
         StartingPos 8
         i len 8
      ;

      keep VarName MaxLength StartingPos;

      /* Array to keep already found max length of each variable, 
       * using _temporary_ takes care of "retaining" the values. */
      array maxlen [&amp;amp;varCount] _temporary_ (&amp;amp;varCount * 0);

      do i = 1 to countw("&amp;amp;varList", " ");
         VarName = scan("&amp;amp;varList", i, " ");
         /* Assuming that at least every numeric variable has a format attached. */
         len = lengthn(vvaluex(VarName));
       
         if len &amp;gt; maxlen[i] then do;
            maxlen[i] = len;
         end;
      end;

      if jobDone then do;
         StartingPos = 1;

         do i = 1 to countw("&amp;amp;varList", " ");
            VarName = scan("&amp;amp;varList", i, " ");
            MaxLength = maxlen[i];
            output;
            /* +1 for the separating blank */
            StartingPos = StartingPos + MaxLength + 1; 
         end;

         call symputx("lineLength", StartingPos-1);
      end;
   run;

   %local exportProgram;
   %let exportProgram = %sysfunc(pathname(work))\export.sas;

   /* This data-null-step creates another data-null-step saved to &amp;amp;exportProgram */
   data _null_;
      set work.Meta end= jobDone;
      file "&amp;amp;exportProgram." ;

      length buffer $ 1000;
      retain buffer;

      if _n_ = 1 then do;
         put 'data _null_;';
         put 'set work.class;';
         /* lrecl + pad =&amp;gt; blanks after the last char */
         put "file '&amp;amp;TargetFile.' lrecl= &amp;amp;lineLength. pad;";
         buffer = 'put';
      end;

      buffer = catx(' ', buffer, cats('@', StartingPos), VarName);

      if jobDone then do;
         put buffer ';';
         put 'run;';
      end;
   run;

   %include "&amp;amp;exportProgram.";

%mend;

data work.class;
   set sashelp.class;

   format
      Age 2.
      Height 5.1
      Weight 5.1
   ;
run;

options mprint;

%WriteDatasetToFile(
   SourceDataset= work.class,
   TargetFile= CHANGE_PATH\class.txt
);

options nomprint;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Jan 2018 14:31:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424571#M104561</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-01-03T14:31:28Z</dc:date>
    </item>
    <item>
      <title>Re: output fixed length and record the position and length of a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424579#M104563</link>
      <description>&lt;P&gt;unfortunately &amp;nbsp;do not have&amp;nbsp;the variables formatted.&lt;/P&gt;
&lt;P&gt;however&amp;nbsp;great job. thank you very much.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jan 2018 14:55:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-fixed-length-and-record-the-position-and-length-of-a/m-p/424579#M104563</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2018-01-03T14:55:56Z</dc:date>
    </item>
  </channel>
</rss>

