DATA Step, Macro, Functions and more

Leading zero of my character string is dropped

Accepted Solution Solved
Reply
Regular Contributor
Posts: 212
Accepted Solution

Leading zero of my character string is dropped

Hi. The resulting dataset stores the 5 character imb_dlvry_zip_5 without leading zeros. So an imb_dlvry_zip_5 of 02367 is stored as 2367. The result is that the following Proc Print also outputs the imb_dlvry_zip_5 value without the leading zero.  How can I prevent it from doing this?  I'd appreciate any help.

 

proc sql;
 create table HighsAndLows as 
 select imb_dlvry_zip_5, rule_nm, rule_order, percent, total
 from myDSs.QueryDataSummary&QTR
 where imb_dlvry_zip_5 <> '164'
   and rule_nm NOT in('TOTAL BIDS VOLUME SAMPLED')
 group by rule_nm
 having total = max(total) or total = min(total)
 order by rule_order, rule_nm, total, imb_dlvry_zip_5;
quit;

proc print label noobs;
label imb_dlvry_zip_5 = 'ZIP5';
run;

Accepted Solutions
Solution
‎11-11-2016 01:17 PM
Super User
Super User
Posts: 7,977

Re: Leading zero of my character string is dropped

Posted in reply to buechler66

Yes, then this is a well known problem.  It is not SAS however, but Excel which trims off leading zeros of numerical data, that is its default behaviour.  To get around this you need to tell it, via tags, that it should treat the number as character (you will have seen this with the small gree triangle in the cell):

proc report data=<your dataset> nowd;
  columns _all_;
  define imb_dlvry / "Label for variable" style={tagattr='format:text'};
run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,977

Re: Leading zero of my character string is dropped

Posted in reply to buechler66

Please post example data of what you have.  If IMB_DLVRY is indeed character in the table you take the data from then it would be character in the output as well, hence it would retain the "0" at the front.  If however it is numeric, then the default is to not have leading zeros as they are meaningless.  You can use Zx. format if you really need them, but Excel and things will remove them also unless you stipulate text.

Regular Contributor
Posts: 212

Re: Leading zero of my character string is dropped

 

I'v traced the imb_dlvry_zip_5 variable from the original permanent ds to the temporary ds following the Proc SQL and it remains a character value throughout and retains the leading zero.

 

So it now seems as tho it's my output using ODS that may be causing my issue.  Sorry I didn't include this in the original post. Any ideas what may be happening here?

 

/*	Output to XML Excel ready format */
ods tagsets.excelxp file="c:\PTH_&QTR._Highs_and_Lows.xml" 
						style=statistical options(orientation='portrait' gridlines='yes' sheet_name="&QTR" embedded_titles='yes');
title j=left "PTH Highs and Lows for Quarter &QTR";
proc print label noobs;
label imb_dlvry_zip_5 = 'ZIP5';
run;
ods tagsets.excelxp close;

 


Capture.JPG
Solution
‎11-11-2016 01:17 PM
Super User
Super User
Posts: 7,977

Re: Leading zero of my character string is dropped

Posted in reply to buechler66

Yes, then this is a well known problem.  It is not SAS however, but Excel which trims off leading zeros of numerical data, that is its default behaviour.  To get around this you need to tell it, via tags, that it should treat the number as character (you will have seen this with the small gree triangle in the cell):

proc report data=<your dataset> nowd;
  columns _all_;
  define imb_dlvry / "Label for variable" style={tagattr='format:text'};
run;
Regular Contributor
Posts: 212

Re: Leading zero of my character string is dropped

Hmm sounds promising, but I must be doing something wrong still.

 

ods tagsets.excelxp file="c:\PTH_&QTR._Highs_and_Lows.xml" 
style=statistical options(orientation='portrait' gridlines='yes' sheet_name="&QTR" embedded_titles='yes');
title j=left "PTH Highs and Lows for Quarter &QTR";
proc print noobs;
columns _all_;
define imb_dlvry_zip_5 / "ZIP5" style={tagattr='format:text'};
run;
ods tagsets.excelxp close;

 

379 ods tagsets.excelxp file="c:\PTH_&QTR._Highs_and_Lows.xml"
380 style=statistical options(orientation='portrait' gridlines='yes' sheet_name="&QTR"
380! embedded_titles='yes');
NOTE: Writing TAGSETS.EXCELXP Body file: c:\PTH_164_Highs_and_Lows.xml
NOTE: This is the Excel XP tagset (Compatible with SAS 9.1.3 and above, v1.122, 01/04/2011). Add options(doc='help') to the
ods statement for more information.
382 title j=left "PTH Highs and Lows for Quarter &QTR";
384 proc print noobs;
385 columns _all_;
-------
180
386 define imb_dlvry_zip_5 / "ZIP5" style={tagattr='format:text'};
------
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
388 run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 564.67k
OS Memory 21644.00k
Timestamp 11/11/2016 10:29:58 AM

389 ods tagsets.excelxp close;

 

 

Super User
Super User
Posts: 7,977

Re: Leading zero of my character string is dropped

Posted in reply to buechler66

Look at the difference between your program and mine, particularly the "proc report" versus "proc print" as they have different syntax.  Personally I would use report as its more flexible.

Regular Contributor
Posts: 212

Re: Leading zero of my character string is dropped

Thanks so much for the help. I appreciate your time.

Super User
Posts: 7,832

Re: Leading zero of my character string is dropped

Posted in reply to buechler66

To display numeric values with leading zeroes, use the Zw. format. To store such values, you need to put them into a character variable, using the same Zw. format.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 429 views
  • 3 likes
  • 3 in conversation