Help using Base SAS procedures

Correcting numeric to character error

Reply
Contributor
Posts: 59

Correcting numeric to character error

data loans2;

16246     +  length ln_no $10;

16247     +

16248     +  set loans;

16249     +  if Account__ = . then ln_no = ' ';

16250     +  else ln_no = put(Account__,z10.);

                                        ____

                                        48

ERROR 48-59: The format $Z was not found or could not be loaded.

This error occurs using the account number. This is an attempt to treat the Account_ as a character.  Not sure why I am getting the error

Super User
Super User
Posts: 6,500

Re: Correcting numeric to character error

ACCOUNT__ must be already defined in the dataset LOANS as a character variable.

ln_no = put(input(Account__,??10.),z10.);

ln_no = compress(ln_no,'.');

Contributor
Posts: 59

Re: Correcting numeric to character error

  I ended up using the following:

data loans2;

  length ln_no $10;

  set loans;

  if Account__ = . then ln_no = ' ';

else ln_no = put(input(Account__,??10.),z10.);

run;

The program ran ok and I get output.  In a few cases I get an error message due to very large account numbers.  See example.  Was I also supposed to use

ln_no = compress(ln_no,'.');

If so, would that account for account numbers that are extremely large.  How would I plug that part of the code into the one I show here?  The report runs on a unix server.  Since I get output and the program does not stop, I may still be ok but wanted to get rid of the error

NOTE: Invalid numeric data, ACCOUNT__='21,500,002,156,297,200' , at line 95 column 6.

Super User
Super User
Posts: 6,500

Re: Correcting numeric to character error

The error message is from this line expression:

Account__ = .


Suppression of that error message is why I reversed your logic.  My example will generate the variable LN_NO for all values of account. The ?? in front of the INFORMAT will suppress the error message. The other advantage of doing the PUT() function first is that if LN_NO is not defined in the input dataset then SAS will know to create it as a character variable of length 10 based on the use of Z10. format.

If the value in ACCOUNT__ is invalid then the INPUT() function will return missing and the PUT() function should spit it out as a period in position 10. (depending of the setting of the option MISSING).  So the COMPRESS() function will convert the period to a space.

You might need more complex logic if the variable ACCOUNT__ is longer than $10.  Using INPUT(ACCOUNT__,10.) will only read the first 10 characters.  So if ACCOUNT__ is twelve digits long it will ignore the last two, so '000000000100' would be converted to 1 instead of 100.

Ask a Question
Discussion stats
  • 3 replies
  • 587 views
  • 3 likes
  • 2 in conversation