Can you keep leading zeros in proc export when creating an excel file. Don't want to create a cvs file or use proc print with ODS
If it's a character variable it should stick. Formats are not passed to the Excel file.
Otherwise ODS Excel is your best option.
Add a TAB character before it, then proc export.
x=cats('09'x,x);
I agree with @Reeza. The leading zeros should stay if you make it a character variable. e.g.:
data have; input var1; format var1 z6.; format var2 $6.; var2=put(var1,z6.); cards; 1 2 3 4 5 6 7 8 9 ; proc export data=have file='/folders/myfolders/want.xlsx' replace dbms=xlsx; sheet='Sheet1'; run;
Var2 should export as desired.
Art, CEO, AnalystFinder.com
You don't need all that 'proc export code'
* keep leading zeros;
libname xel clear; * just in case you forget;
%utlfkil(d:/xls/have.xls); * delete it;
libname xel "d:/xls/have.xls";
data xel.have;
input var1;
format var1 z6.;
format var2 $6.;
var2=put(var1,z6.);
cards4;
1
2
3
4
5
6
7
8
9
;;;;
run;quit;
libname xel clear;
* Keep leading blanks;
* this presevers leading blanks;
%utlfkil(d:/xls/have.xls);
libname xel "d:/xls/have.xls";
libname xel "d:/xls/have.xls";
data xel.have;
input var1 $char4.;
cards4;
1
2
3
4
5
6
7
8
9
;;;;
run;quit;
libname xel clear;
@djrisks Alternatively use ODS Excel which gives you much more control. ...and it respects SAS formats "out of the box".
data class;
set sashelp.class;
format age z10.;
run;
ods excel
file='c:\temp\test.xlsx'
;
proc print data=class noobs;
run;
ods excel close;
This worked for me thanks.
Try add a special STYLE:
data class;
set sashelp.class;
_age=put(age,z8. -l);
drop age;
run;
ods excel file='c:\temp\test.xlsx' ;
proc report data=class nowd;
define _age/display style={tagattr='type:text format:@'};
run;
ods excel close;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.