I have a variable that is character that I am trying to convert numeric and ensure leading zeros. I have attempted the following, but get the message: Format $Z was not found or could not be loaded.
length ocounty $2. oid $6.; if NCRPV1 ne . then ocounty = strip(put(NCRPV1,z2.));
Can someone help resolve this?
Try to make you numeric and then convert back to char using Z format
strip(put(input(NCRPV1,32.),z2.));
You can't apply a Z format for a char value. You need your value to standard numeric data. So you need INPUT function first
To convert character to numeric, you need the INPUT function.
ocounty = input(NCRPV1,2.);
format ocounty z2.;
But if the data is some kind of a code for something, keep it as character.
Character variables require character formats. So the data step compiler converted the attempt to use the numeric Z format with the character variable NCRPV1 into a request for the character format $Z. SAS does not delivery such a format and it couldn't find a user defined one either.
You first need to figure out whether NCRPV1 is supposed to be numeric or character in order to decide if you need to change your program or change your data.
The error suggests your variable NCRPV1 is already character. If you want to add leading zeros then you will need to do this:
if NCRPV1 ne '' then ocounty = strip(put(input(NCRPV1, 2.),z2.));
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.
Ready to level-up your skills? Choose your own adventure.