Help using Base SAS procedures

Export SAS Numeric Characters to CSV

Accepted Solution Solved
Reply
Regular Contributor
Posts: 220
Accepted Solution

Export SAS Numeric Characters to CSV

I am exporting from SAS to Excel (CSV).  I have three-digit subject numbers such as 001, 002, 003, etc.  When I run the PROC EXPORT, I get 1, 2, 3, etc. for that field.  Is there an option I can specify to keep it as a three-digit character string in Excel?


Accepted Solutions
Solution
‎02-06-2012 11:14 AM
Super User
Super User
Posts: 6,502

Export SAS Numeric Characters to CSV

Short answer, NO.  CSV files are just text files.  It is Excel that is "eating" the leading zeros.

Long answer, There are a few methods that help.

For occasional use change the way that you are opening the file in Excel so that you get the option screens that let you tell it the column is character even though the values look like numbers.  The easist way to get Excel to prompt you is to not use .csv as the file extension.

There are other ways of exporting to Excel, such as using ODS tagsets.EXCELXP, that give you control over the format of the cells.  But then they are not CSV files and so can not be read by programs other than Excel.

View solution in original post


All Replies
Solution
‎02-06-2012 11:14 AM
Super User
Super User
Posts: 6,502

Export SAS Numeric Characters to CSV

Short answer, NO.  CSV files are just text files.  It is Excel that is "eating" the leading zeros.

Long answer, There are a few methods that help.

For occasional use change the way that you are opening the file in Excel so that you get the option screens that let you tell it the column is character even though the values look like numbers.  The easist way to get Excel to prompt you is to not use .csv as the file extension.

There are other ways of exporting to Excel, such as using ODS tagsets.EXCELXP, that give you control over the format of the cells.  But then they are not CSV files and so can not be read by programs other than Excel.

Occasional Contributor
Posts: 9

Export SAS Numeric Characters to CSV

before you export to .csv, add a single quote to the front of the numerics you want to keep as a character string, e.g. '001, '002, '003

Excel should interpret the beginning single quote as a text field marker.

Regular Contributor
Posts: 220

Export SAS Numeric Characters to CSV

This exports the data as '001, '002, etc.  I am able to click on the first cell, hit enter, and it removes the apostraphe.  Then, I drag down.  It works, but it is not automatic.  Thanks!

Valued Guide
Posts: 765

Re: Export SAS Numeric Characters to CSV

hi ... if the eventual destination is Excel, do you need the intervening CSV file or can you go to Excel directly ...

data x;

input x1-x3;

datalines;

1 2 3

10 20 30

100 200 300

;

ods listing close;

ods results off;

ods msoffice2k file='z:\test.xls' ;

proc print data=x noobs;

var x1-x3 / style={htmlstyle="mso-number-format:'00000'"};

run;

ods msoffice2k close;

ods results;

ods listing;

screen capture of TEXT.XLS is attached


test.png
Regular Contributor
Posts: 220

Export SAS Numeric Characters to CSV

I need the CSV format.  I also have a few cases where the ID needs to be 4 digits long, so I don't know that I would be able to use an if statement to make sure that your method placed 4 digits vs. 3 digits when needed.

Valued Guide
Posts: 765

Re: Export SAS Numeric Characters to CSV

hi ... the varying length is not an issue (not sure what you mean about "use an IF statement") ...

data x;

input x1-x3;

datalines;

1 10 100

2 20 200

3 30 300

;

ods listing close;

ods results off;

ods msoffice2k file='z:\test.xls';

proc print data=x noobs;

var x1 / style={htmlstyle="mso-number-format:'00'"};

var x2 / style={htmlstyle="mso-number-format:'000'"};

var x3 / style={htmlstyle="mso-number-format:'0000'"};

run;

ods msoffice2k close;

ods results;

ods listing;


test.png
New Contributor
Posts: 3

Re: Export SAS Numeric Characters to CSV

Another way to keep the leading zeros is to create a tagset based on the CSVALL tagset.  Below is an example of one I created based on an example from Chevell Parker (http://www2.sas.com/proceedings/sugi28/012-28.pdf).   

PROC TEMPLATE;

     DEFINE tagset Tagsets.CSVDownload;

          parent=tagsets.CSVALL;

          DEFINE event data;

               PUT "," / if !cmp(COLSTART , "1");

               PUT '=' """" / if cmp( TYPE ,"string");

               PUT VALUE;

               PUT """" / if cmp( TYPE , "string");

          END;

     END;

RUN;

%LET RV=%SYSFUNC(APPSRV_HEADER(CONTENT-TYPE,TEXT/CSV));

ODS markup BODY=_webout tagset=Tagsets.CSVDownload;

%LET RV=%SYSFUNC(APPSRV_HEADER(CONTENT-DISPOSITION,%STR(ATTACHMENT;FILENAME="DownLoad.csv")));

proc print data=one;

RUN;

ODS markup close;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 4546 views
  • 3 likes
  • 5 in conversation