BookmarkSubscribeRSS Feed
sayanapex06
Obsidian | Level 7

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.

 

 

17 REPLIES 17
Reeza
Super User

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?

sayanapex06
Obsidian | Level 7

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

 

Reeza
Super User

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

 

sayanapex06
Obsidian | Level 7

error :Function CATT unknown and cannot be accessed. 

Reeza
Super User

What version of SAS are you using? 

Post your code and log.

Reeza
Super User

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

sayanapex06
Obsidian | Level 7

Why am I getting :

 

FUNCTION CATT unknown or cannot be accessed error.

 

What are the reasons please tell me.

 

Reeza
Super User

What version of SAS are you using? 

Post your code and log.

sayanapex06
Obsidian | Level 7

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

Reeza
Super User

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. 

 

sayanapex06
Obsidian | Level 7

can you explain point 4

Tom
Super User Tom
Super User

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;
sayanapex06
Obsidian | Level 7

Hi tom,

 

could you show me the output of the program

Reeza
Super User

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"
 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 17 replies
  • 16661 views
  • 1 like
  • 5 in conversation