BookmarkSubscribeRSS Feed
Shah
Obsidian | Level 7

I have tried the above tostring function and it is working fine. Thank you so much for the above solution and also regret for the late reply.

I have one more question to ask, I have the field COCD with the len 1, 2, 3, and 4 digits. The COCD column must consist of 4 len number as per our requirement so i have to prefix each company code with '0' to make it 4 digit. Is there any solution or functionality i can use to change the len to 4 digits?

I have used below the expression but not yielding result:

if len(COCD) == 1 then cocd_text = left(000,COCD)

COCDRequired Output
10001
20002
110011
220022
3330333
44444444


Regards,
Shaheen

1 REPLY 1
skillman
SAS Employee

This was answered here:

but to save you a click here is the solution:

/* Pre-processing Expression*/

string desired_output

hidden integer i

desired_output = ''

i = 0

/*Expression*/

if len(`COCD`) < 4

  begin

  desired_output = `COCD`

  while i <  (4 - len(`COCD`))

  begin

  desired_output = '0' & desired_output

  i = i + 1

  end

  end

else

  desired_output = `COCD`

  i = 0

dm_expression.png

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 713 views
  • 0 likes
  • 2 in conversation