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

 

Hi,

I want to Add zero(0) in prefix wherever the length(Have) less then “3”  and blank as blank.

 

HaveLengthWantLength
11231123
1220123
123455123455
cg.565cg.565
8720873
110013
11231123
1frt.51frt.5
    
e100e3
010003

 

kindly help.

 

Thanks,

Siva

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

both have and want variables should be a char type: (code fixed)

 

length want $10;   /* adapt length to max length of have */

len = length(strip(have));

if len < 3 then want = substr('00',1,3-len) || left(have);

else have = want;

View solution in original post

13 REPLIES 13
Shmuel
Garnet | Level 18

both have and want variables should be a char type: (code fixed)

 

length want $10;   /* adapt length to max length of have */

len = length(strip(have));

if len < 3 then want = substr('00',1,3-len) || left(have);

else have = want;

Astounding
PROC Star

Here's one approach, within a DATA step:

 

select (lengthn(have));

   when (1) want = '00' || have;

   when (2) want = '0' || have;

   otherwise want=have;

end;

data_null__
Jade | Level 19

@sivastat08 wrote:

 

Hi,

I want to Add zero(0) in prefix wherever the length(Have) less then “3”  and blank as blank.

 

Have Length Want Length
112 3 112 3
12 2 012 3
12345 5 12345 5
cg.56 5 cg.56 5
87 2 087 3
1 1 001 3
112 3 112 3
1frt. 5 1frt. 5
       
e 1 00e 3
0 1 000 3

 

kindly help.

 

Thanks,

Siva


How about SUBSTR on the left side of assignment statement equal sign.

 

data l0;
   infile cards missover;
   input Have $ Length1 xWant $ Length2;
   l = lengthn(have);
   if 0 eq l or l ge 3 then want = have;
   else do;
      want = '000';
      substr(want,3-l+1)=have;
      end;
   z = compare(xwant,want);
   cards;
112 3 112 3
12 2 012 3
12345 5 12345 5
cg.56 5 cg.56 5
87 2 087 3
1 1 001 3
112 3 112 3
1frt. 5 1frt. 5
   
e 1 00e 3
0 1 000 3
;;;;
   run;
proc print;
   run;

Capture.PNG

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Simple substr() function will do it:

data want;
  input have $;
  want=substr(cats("00",have),lengthn(cats("00",have))-2);
datalines;
112
12
1
;
run;
Tom
Super User Tom
Super User

@RW9 If you want your solution to support strings longer than 3 characters then add a MIN() function.

data want;
  length have want $10;
  input have $;
  want=substr(cats('00',have),min(3,length(cats('00',have))-2));
  put have= want= ;
datalines;
112x
12
1
;;;;

 

art297
Opal | Level 21

I would change your choice of solution to any one of the other solutions offered, since @Shmuel's proposed solution simply won't work.

 

Art, CEO, AnalystFinder.com

 

Shmuel
Garnet | Level 18

@art297, you are right, I havn't noticed that have can be short alphabetic. My mistake.

data_null__
Jade | Level 19

@Shmuel wrote:

@art297, you are right, I havn't noticed that have can be short alphabetic. My mistake.


@Shmuel you could edit your reply to make it work for the OPs data.

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 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.

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
  • 13 replies
  • 1561 views
  • 9 likes
  • 7 in conversation