Solved
Contributor
Posts: 70

# Add (0) in prefix wherever the length less then “3” for Character variable using sas

[ Edited ]

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

Accepted Solutions
Solution
‎03-07-2017 10:59 AM
Posts: 1,831

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

[ Edited ]
Posted in reply to sivastat08

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;

All Replies
Solution
‎03-07-2017 10:59 AM
Posts: 1,831

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

[ Edited ]
Posted in reply to sivastat08

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;

Contributor
Posts: 70

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

Thanks for the replay.
Super User
Posts: 6,633

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

Posted in reply to sivastat08

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;

Contributor
Posts: 70

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

Posted in reply to Astounding
Thanks for the replay.
Posts: 3,846

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

Posted in reply to sivastat08

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;``````

Contributor
Posts: 70

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

Posted in reply to data_null__
Thanks for the result.
Super User
Posts: 9,423

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

Posted in reply to sivastat08

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;```
Contributor
Posts: 70

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

Thanks for the replay Mr.RW9.
Super User
Posts: 7,934

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

[ Edited ]

@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
;;;;``````

PROC Star
Posts: 8,149

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

Posted in reply to sivastat08

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

Posts: 1,831

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

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

Posts: 3,846

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

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.

Posts: 1,831

## Re: Add (0) in prefix wherever the length less then “3” for Character variable using sas

Posted in reply to data_null__

@data_null__, thanks.

I have edited the post and fixed it.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 13 replies
• 224 views
• 9 likes
• 7 in conversation