BookmarkSubscribeRSS Feed
bhca60
Quartz | Level 8
I am using SAS VIYA and trying to convert a variable “ID” (data from an excel import) from numeric to character. I am trying to join it to the “ID” in another data set to bring back any matches.

I’ve done this but it brings nothing back and want to make sure if that’s because there are no matches or because something is wrong with my input:

Data c;
Set b;
ID_char=put(ID,8.);
Run;
14 REPLIES 14
bhca60
Quartz | Level 8
 The ID is a six digit  number 123456
Reeza
Super User
Try adding the -l component and trim in case you have blanks?

ID_Char = put(trim(ID), 8. -l);

(PS not that familiar with Viya)
bhca60
Quartz | Level 8
It looks like it worked but there is an ID that has a leading zero like 099999 but still shows as 99999.
ballardw
Super User

@bhca60 wrote:
It looks like it worked but there is an ID that has a leading zero like 099999 but still shows as 99999.

Shows where?

 

If you send values to a spreadsheet quite often the rules of the spreadsheet will convert text containing digits to numeric which loses the leading zeros.

bhca60
Quartz | Level 8
Right but I imported it to sas from excel and thought that it would include a 0 in the output after conversion to character.
Kurt_Bremser
Super User

Numbers do not have leading zeroes. You must force the display of leading zeroes by using the proper format during the conversion to character.

bhca60
Quartz | Level 8
And what if you want to remove leading zeros but keep it a character?
Reeza
Super User
ID_char = put(input(id_char, 8.), 8. -l)
Reeza
Super User

Then you can use the Z6 format in your conversion.

 

ID_char = put(trim(ID), z6. -l);
Tom
Super User Tom
Super User

@Reeza wrote:

Then you can use the Z6 format in your conversion.

 

ID_char = put(trim(ID), z6. -l);

Do not use the TRIM() function with a NUMERIC variable. 

There is no need to use the -L modifier with the Z format as there will not be any leading spaces to remove.

ID_char = put(ID, z6.);

 

SASKiwi
PROC Star

@bhca60  - It is normally better to fix this problem in Excel and then re-import. Open Excel, select the ID column. In the Data tab, select the Text to Column wizard, and go through the dialog, selecting Text as your column type. Now, when you re-import into SAS your ID column type will stay as character.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 14 replies
  • 1548 views
  • 0 likes
  • 6 in conversation