BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
djbateman
Lapis Lazuli | Level 10

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

7 REPLIES 7
Tom
Super User Tom
Super User

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.

ChuckMoore
Calcite | Level 5

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.

djbateman
Lapis Lazuli | Level 10

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!

MikeZdeb
Rhodochrosite | Level 12

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
djbateman
Lapis Lazuli | Level 10

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.

MikeZdeb
Rhodochrosite | Level 12

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
TG_WPAFB
Calcite | Level 5

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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