DATA Step, Macro, Functions and more

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

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

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.

 

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

 

kindly help.

 

Thanks,

Siva


Accepted Solutions
Solution
‎03-07-2017 10:59 AM
Trusted Advisor
Posts: 1,394

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

[ Edited ]

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


All Replies
Solution
‎03-07-2017 10:59 AM
Trusted Advisor
Posts: 1,394

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

[ Edited ]

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: 55

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

Thanks for the replay.
Super User
Posts: 5,085

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

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: 55

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

Thanks for the replay.
Respected Advisor
Posts: 3,777

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


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

Contributor
Posts: 55

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

Thanks for the result.
Super User
Super User
Posts: 7,407

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

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: 55

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

Thanks for the replay Mr.RW9.
Super User
Super User
Posts: 6,502

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: 7,363

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

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

 

Trusted Advisor
Posts: 1,394

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.

Respected Advisor
Posts: 3,777

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.

Trusted Advisor
Posts: 1,394

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

@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
  • 174 views
  • 9 likes
  • 7 in conversation