BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have a filed that will be 9 to 12 numbers in length, example 954265478. The field is in text format. I need to pad the front of this number with 0 to make the total length of the field 18, 000000000954265478. The length of my initial data wont always be 9, it can vary. Does anyone have a suggestion on how I can do this?
7 REPLIES 7
deleted_user
Not applicable
The easiest that I can think of is:

data old_data;
length number $18;
set old_data;

number = put(input(number,18.),z18.);
run;
quit; null
deleted_user
Not applicable
Thank you very much
deleted_user
Not applicable
Your welcome.

As a side note:

It would be more efficient to simply store a number as a number, and then associate an appropriate format for that number with it. A number is stored in binary, and in SAS, at present (per my understanding), consumes at most 8 bytes. Your 18 character text number consumes 18 bytes. All you need is a format associated with the number to make it report as 18 characters, padded with leading zeroes.

DATA table;
set table;
format number z18.;
run;
quit;

or

DATA table;
attrib number format=z18. informat=18.;
infile input_data;
input number;
run;
quit;

both the format and attrib statements can associate the z18. format to a field.
then whenever SAS reports the value, it will print it as an 18 character ...


Using this method allows you to simply change the data representation with a format, as opposed to having to actually run through the data to change the textual representation.

Formats can even impose a picture to a number:
.ssn takes a 9 digit number and reports it as a social security number = 123-45-6789
you could create a credit card format that injects dashes or spaces into a 16 digit number.

Similarly informats can translate a textually entered "number" into a number.
(312) 555-1234 could be input directly and stored as a binary number = 3125551234, and then reprinted as a European style phone number = 312.555.1234

I would recommend reading the documentation for the format and attrib statements, and the proc format procedure. null
deleted_user
Not applicable
If i use the format z18. and keep the field as a number will a Merge of that field or a Proc sql Left Join recognize this field if the field i am joining it with is text?
deleted_user
Not applicable
I don't know.

Ideally, all the data instances of a number are numeric. But we can't always rely on that.

If I were in this situation, I would have to experiment a bit.

I do know that SAS can do and sometimes will do data conversions for numbers into text, and text into numbers, to match datatypes in conditional clauses.
deleted_user
Not applicable
Chuck;

This is the error message I get when I try and do a JOIN with a number field formated at z18. and a text field length $18

ERROR: Expression using equals (=) has components that are of different data types.
deleted_user
Not applicable
an SQL join can convert data types "on the fly".
To achieve the same in a data step merge, use data step views to convert to the common data type.

I hope you don't need to rely on a SAS numeric holding 18 digits precision.

on z/OS, see
http://support.sas.com/onlinedoc/913/getDoc/en/hosto390.hlp/mvs-length-length.htm
There it declares 16 digits

On windows the corresponding table is different because the the storage form is different. The result is that the table at http://support.sas.com/onlinedoc/913/getDoc/en/hostwin.hlp/numvar.htm offers only to retain precision for 15 digits.

As SAS on unix uses the same internal storage as on windows, it is no surprise that the limitations are declared the same, in the table at http://support.sas.com/onlinedoc/913/getDoc/en/hostunx.hlp/a000344718.htm .


So, if you want to rely on 18 digits precision, either hold it as 18 characters, 9 bytes of unsigned packed numeric, or some extremely clever binary combination.

Certainly do not rely on retrieving any 18 digits you store in a SAS numeric variable. Of course, you may be lucky !


Good Luck

PeterC

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2365 views
  • 0 likes
  • 1 in conversation