DATA Step, Macro, Functions and more

Adding leading zeros to numeric,alphanumeric and character variables

Reply
Contributor
Posts: 37

Adding leading zeros to numeric,alphanumeric and character variables

Hi All,

 

This is an urgent requirement that I need to fulfil, would highly appreciate it if you help on this.

 

Question :

I have data like :

data test;
    length A $ 4 ;
    infile datalines;
    input A;
    A = put(input(A,best4.),z4.);
datalines;81
1034
91
A46
6HJ
AAAAAA
F39000
DSUG08
CSDF85
000000
;
run;

I want the output like :

000081

001034

000091

000A46

0006HJ

AAAAAA

F39000

DSUG08

CSDF85

000000

 

which is basically I need to add leaing 'zeros' if length is less than 6 but for all 6 length characters I need the same output unaltered. The desired output is given above

 

I need a particular format for doing this.

I dont need a temporary fix on this

I need a permanent solution as to whatever value comes for the field 'A' I get the output like stated above.

 

Codes I tried :

 

A = put(input(A,best4.),z4.);   ------- case 1
and
A = cats(repeat('0',6-length(A)-1), x); ------- case 2

case1 :

Only the numeric records are getting populated with leading zeros and for rest it is coming as 0

case2 :

I am getting the desired output as stated above but for some cases I am getting error like :

'Function CATS unknown or cannot be accessed.'

 Please provide me with a better format for doing this.

 

 

Super User
Posts: 17,863

Re: Adding leading zeros to numeric,alphanumeric and character variables

Is your data exactly as posted in the data step? You have 81 right after the datalines statement.

The last record is longer than 6 characters so it has to get separated?

How do you know the rules for separation, assume 6 characters per variable?

What happens if the last variable is short when splitting it up, ie last set is only 3 chars?

 

You have a mix of character/numeric so INPUT and Z6 format won't work. 

 

And if you need a length of 6 why are you using 4 in the code?

Contributor
Posts: 37

Re: Adding leading zeros to numeric,alphanumeric and character variables

Hi reeza

 

Input:

 

91

81

aaaaaa

bbbbbb

cfg014

456asd

4kj

gh4

0

 

output :

 

000091

000081

aaaaaa

bbbbbb

cfg014

456asd

0004kj

000gh4

000000

 

I have tried both :

 

padded = put(input(charvar,best6.),z6.);

 and

xx = cats(repeat('0',6-length(x)-1), x); 

 

cats somewhere is fine and somewhere is giving the error :

 

"unknown func CATS and cannot be accessed" but with CATS i am getting the desired output

 

With the first thing only numeric values are getting populated with eading zeros rest are being changed to 0

 

Please give me a better format or an alternative for CATS/X/T/Q

 

Super User
Posts: 17,863

Re: Adding leading zeros to numeric,alphanumeric and character variables

This works:

 

data test;
    length A $6. ;
    infile datalines;
    input A $;
    b = catt(repeat('0',6-length(a)-1),a);
datalines;
81
91
81
aaaaaa
bbbbbb
cfg014
456asd
4kj
gh4
0
;
run;

proc print data=test;
run;

If you want to build yourself a custom function see the answer to this similar question here:

https://communities.sas.com/t5/Base-SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to...

 

Contributor
Posts: 37

Re: Adding leading zeros to numeric,alphanumeric and character variables

error :Function CATT unknown and cannot be accessed. 

Super User
Posts: 17,863

Re: Adding leading zeros to numeric,alphanumeric and character variables

What version of SAS are you using? 

Post your code and log.

Super User
Posts: 17,863

Re: Adding leading zeros to numeric,alphanumeric and character variables

This works for sure, no errors in my log. 

if length(a) = 6 then b=a;
else b=catt(repeat('0', 6-length(a)-1, a);

Contributor
Posts: 37

Re: Adding leading zeros to numeric,alphanumeric and character variables

Why am I getting :

 

FUNCTION CATT unknown or cannot be accessed error.

 

What are the reasons please tell me.

 

Super User
Posts: 17,863

Re: Adding leading zeros to numeric,alphanumeric and character variables

What version of SAS are you using? 

Post your code and log.

Contributor
Posts: 37

Re: Adding leading zeros to numeric,alphanumeric and character variables

I am using SAS in zOS i.e in mainframe.

 

When I am trying the syntax :

 

xx = cats(repeat('0',6-length(x)-1), x);

 

It works fine when I am reading from datalines and giving any random inputs. It is working perfectly fine without any errors.

 

But when I am reading a ~ delimitted file and there is a field which is of $6. And when I am trying to conver that field to 

 

xx = cats(repeat('0',6-length(x)-1), x);

 

I get the error :

 

FUCNTION CATS not found or cannot be accessed.

 

But when I am reading from datalines I am not getting the error..

Super User
Posts: 17,863

Re: Adding leading zeros to numeric,alphanumeric and character variables

1. Either you have an error before that's causing the issue with the function

2. Your SAS version is old and doesn't support the CATT functions.

3. It's not available in Z/OS for some reason - seems unlikely but possible. 

4. It's not available via pass through if you're using that

5. You're not using SAS but using a different system (WPS) that doesn't support it

6. Something else that you're likely not conveying in the question here. 

 

Roll a dice I guess. 

 

Contributor
Posts: 37

Re: Adding leading zeros to numeric,alphanumeric and character variables

can you explain point 4

Super User
Super User
Posts: 6,502

Re: Adding leading zeros to numeric,alphanumeric and character variables

If you can't use CATT() function then use something else.

You could use the SUBSTRN() function.

Or you could use REVERSE(), TRIM() and SUBSTR().

Or make up your own.

data x;
  input a $6. ;
cards;
123
abcdef
;

data want ;
 set x;
 b=substrn('000000',1,6-length(a))||a;
 c=reverse(substr(reverse(trim(a))||'000000',1,6));
 put (_all_) (=$quote.);
run;
Contributor
Posts: 37

Re: Adding leading zeros to numeric,alphanumeric and character variables

Hi tom,

 

could you show me the output of the program

Super User
Posts: 17,863

Re: Adding leading zeros to numeric,alphanumeric and character variables

Output from @Tom's code:

 

64
65 data want ;
66 set x;
67 b=substrn('000000',1,6-length(a))||a;
68 c=reverse(substr(reverse(trim(a))||'000000',1,6));
69 put (_all_) (=$quote.);
70 run;
 
a="123" b="000123" c="000123"
a="abcdef" b="abcdef" c="abcdef"
 
Ask a Question
Discussion stats
  • 17 replies
  • 278 views
  • 0 likes
  • 5 in conversation