BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fgillen1
Fluorite | Level 6

Hi all,

 

I am looking to add a leading '0' to variables within a field that are less that 5 characters in length. Does anyone know of a bit of code that can achieve this? Below is my attempt. I know everything after 'THEN' works fine, its the selecting variables with less than 5 characters that is hindering me.

 

CASE
WHEN substr(t1.ded_code,max(1,length(t1.ded_code)-3)) THEN PUT(INPUT(t1.ded_code, best.), z2.)
ELSE t1.ded_code
END
AS ded_code_v2

 

Regards

Finbar

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You can simplify it by using SAS functions:

case when lengthn(T1.DED_CODE) < 5 then cats(repeat("0",5-lengthn(T1.DED_CODE)),T1.DED_CODE)
     else T1.DED_CODE end as DED_CODE_V2

So if the trimmed lenght is less than five concatenate 0 for each space less than five.  Note, if DED_CODE is numeric, you could simplify this further by:

put(input(T1.DED_CODE,best.),z5.) as DED_CODE_V2

The Z format padds out numbers with preceeding zeroes.

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You can simplify it by using SAS functions:

case when lengthn(T1.DED_CODE) < 5 then cats(repeat("0",5-lengthn(T1.DED_CODE)),T1.DED_CODE)
     else T1.DED_CODE end as DED_CODE_V2

So if the trimmed lenght is less than five concatenate 0 for each space less than five.  Note, if DED_CODE is numeric, you could simplify this further by:

put(input(T1.DED_CODE,best.),z5.) as DED_CODE_V2

The Z format padds out numbers with preceeding zeroes.

fgillen1
Fluorite | Level 6

Hi,

 

Thanks! That works fine, I just changed 

cats(repeat("0",5-lengthn(T1.DED_CODE))

to

cats(repeat("0",4-lengthn(T1.DED_CODE))

 the first one was throwing in two leading '0's

 

Regards

Finbar

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!

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
  • 2 replies
  • 2560 views
  • 1 like
  • 2 in conversation