<?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: Utility to produce consistent column order CSV file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Utility-to-produce-consistent-column-order-CSV-file/m-p/244507#M45568</link>
    <description>&lt;P&gt;I think SAS's DS2CSV macro may do this for you as you can specify a list of variables that both orders and selects the columns in the CSV:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lebaseutilref/64791/HTML/default/viewer.htm#n0yo3bszlrh0byn1j4fxh4ndei8u.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lebaseutilref/64791/HTML/default/viewer.htm#n0yo3bszlrh0byn1j4fxh4ndei8u.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 19 Jan 2016 18:32:33 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2016-01-19T18:32:33Z</dc:date>
    <item>
      <title>Utility to produce consistent column order CSV file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Utility-to-produce-consistent-column-order-CSV-file/m-p/244505#M45567</link>
      <description>&lt;P&gt;Does anyone have a simple utility to consistently produce CSV file from a SAS dataset. &amp;nbsp;In particular I am looking for methods that would allow the column order in the CSV file to be consistent, even if the source SAS dataset has the columns defined in a different order. &amp;nbsp;Perhaps something with a metadata file that describes the fields and included the column order.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2016 18:21:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Utility-to-produce-consistent-column-order-CSV-file/m-p/244505#M45567</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-01-19T18:21:24Z</dc:date>
    </item>
    <item>
      <title>Re: Utility to produce consistent column order CSV file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Utility-to-produce-consistent-column-order-CSV-file/m-p/244507#M45568</link>
      <description>&lt;P&gt;I think SAS's DS2CSV macro may do this for you as you can specify a list of variables that both orders and selects the columns in the CSV:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lebaseutilref/64791/HTML/default/viewer.htm#n0yo3bszlrh0byn1j4fxh4ndei8u.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lebaseutilref/64791/HTML/default/viewer.htm#n0yo3bszlrh0byn1j4fxh4ndei8u.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2016 18:32:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Utility-to-produce-consistent-column-order-CSV-file/m-p/244507#M45568</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2016-01-19T18:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: Utility to produce consistent column order CSV file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Utility-to-produce-consistent-column-order-CSV-file/m-p/244518#M45570</link>
      <description>&lt;P&gt;Thanks. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So now I just need a reasonable method to generate the variable names in the proper order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one way that I thought of. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I had metadata that listed variables in order for each target file. &amp;nbsp;Perhaps something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data_dictionary;
    infile cards truncover ;
    input table :$32. name :$32. @ ;
    do varnum=1 by 1 until (name=' ');
       output;
       input name :$32. @;
    end;
cards;
TABLE1 name sex age height weight
TABLE2 name age height weight sex 
;;;;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then I could create a wrapper for the %DS2CSV macro that let the user specify the input dataset, output CSV file and which standard table it represents.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro standard_csv
(data=  /* Input Dataset name */
,csvfile= /* Output CSV file name */
,table=   /* Standard Table name */.
,metadata=data_dictionary
);
%local varlist ;
proc sql noprint ;
  select name into :varlist separated by ' '
    from &amp;amp;metadata
    where upcase(table) = %upcase("&amp;amp;table")
    order by varnum 
  ;
quit;
%ds2csv (data=&amp;amp;data, runmode=b, csvfile=&amp;amp;csvfile,var=&amp;amp;varlist)
%mend standard_csv;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So then if I call it with %standard_csv(data=sashelp.class,csvfile=file1.csv,table=TABLE2) I would get a file like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;"Name","Age","Height","Weight","Sex"
"Alfred","14","69","112.5","M"
"Alice","13","56.5","84","F"
"Barbara","13","65.3","98","F"
"Carol","14","62.8","102.5","F"
"Henry","14","63.5","102.5","M"
"James","12","57.3","83","M"
"Jane","12","59.8","84.5","F"
"Janet","15","62.5","112.5","F"
"Jeffrey","13","62.5","84","M"
"John","12","59","99.5","M"
"Joyce","11","51.3","50.5","F"
"Judy","14","64.3","90","F"
"Louise","12","56.3","77","F"
"Mary","15","66.5","112","F"
"Philip","16","72","150","M"
"Robert","12","64.8","128","M"
"Ronald","15","67","133","M"
"Thomas","11","57.5","85","M"
"William","15","66.5","112","M"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jan 2016 19:25:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Utility-to-produce-consistent-column-order-CSV-file/m-p/244518#M45570</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-01-19T19:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: Utility to produce consistent column order CSV file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Utility-to-produce-consistent-column-order-CSV-file/m-p/244572#M45578</link>
      <description>&lt;P&gt;Why not creat a VIEW, it didn't cost you anything ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create view standard as
 select weight,name,age
  from sashelp.class;
quit;
proc export data=standard outfile='/folders/myfolders/x.csv' dbms=csv replace;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Jan 2016 01:35:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Utility-to-produce-consistent-column-order-CSV-file/m-p/244572#M45578</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-01-20T01:35:25Z</dc:date>
    </item>
  </channel>
</rss>

