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

Hello,

 

First of all, i'm sorry for my english, i will excuse myself every time i'm writting a topic !!

 

Well, i'm trying to add zeros on my character variable of size 5  if it is less than 5 characters.

 

Example :

1 : 123     => 00123

2 : 12345 => 12345

3 : 1         => 00001

 

I have a solution that i write but maybe it can be easier.

 

This is what i did :

 

data cim_tset2 ;
set cim_tset1 ;
/* the original variable is "agence" which is in $7. */ format agence1 z5. ; agence1 = put(input(agence,5.),$z5.); cle=compress(code_banque!!"_"!!agence1); run ; data cim_tset3 (rename = (agence1 = agence)) ;
set cim_tset2 (drop = agence);
run ;

I'm using the "Z", so i have to convert my variable into a numeric variable. Then, i'm converting my variable again into a char variable to conserve the ZEROS when i want to compress.

 

Finally, I have to create another DATA SET for rename and drop the new variable i have created..

 

I tried to not create a new variable but each time i'm trying, it make a mistake "the format Z is not know blabla". I don't know if there is better solution than mine, i let you tell me !

 

I'm really sorry for my english, i hope you understood something :X

 

Good afternoon ! 🙂

 

Onizuka

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You have  problem here. By assigning a numeric format (z5.), you implicitly define agence1 as numeric.

Instead of the format statement, give it a length:

length agence1 $5;

and modify your put() function to use a numeric output format:

agence1 = put(input(agence,5.),z5.);

 

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

You have  problem here. By assigning a numeric format (z5.), you implicitly define agence1 as numeric.

Instead of the format statement, give it a length:

length agence1 $5;

and modify your put() function to use a numeric output format:

agence1 = put(input(agence,5.),z5.);

 

Onizuka
Pyrite | Level 9

Hello KurtBremser, thank you for your answer !

 

I have to say that i'm a little bit lost with the formats / informats / length, i think that i should take a look on documentation about it.

 

I did what you say but instead creating a new variable (agence1), i use the existant one (agence) and it's working perfectly. I still have a little question to ask : Why my variable agence (the original which i have transformed right now) is in $7.  when i'm clicking on it ?

and not in $5. ?

 

data cim_tset2 ;
set cim_tset1 ;
length agence $5. ; /*my original variable*/
agence = put(input(agence, 5.), z5.);
cle=compress(code_banque!!"_"!!agence);
run ;
ballardw
Super User

@Onizuka wrote:

Hello KurtBremser, thank you for your answer !

 

I have to say that i'm a little bit lost with the formats / informats / length, i think that i should take a look on documentation about it.

 

I did what you say but instead creating a new variable (agence1), i use the existant one (agence) and it's working perfectly. I still have a little question to ask : Why my variable agence (the original which i have transformed right now) is in $7.  when i'm clicking on it ?

and not in $5. ?

 

data cim_tset2 ;
set cim_tset1 ;
length agence $5. ; /*my original variable*/
agence = put(input(agence, 5.), z5.);
cle=compress(code_banque!!"_"!!agence);
run ;

If your original variable AGENCE is in CIM_TSET1 it will continue to have the format previously assigned.

Add a FORMAT statement in the data step if you want to change the default format.

 

Formats are very flexible and you can change the format for use in any practically any procedure.

For instance:

Proc print data=cim_tset2;
   var agence;
   format agence $2.;
run;

Would print only the first 2 characters of agence. The assigned format has not changed, just what is displayed.

Onizuka
Pyrite | Level 9

Thank you for your answer Ballardw 🙂

Kurt_Bremser
Super User

@Onizuka wrote:

Hello KurtBremser, thank you for your answer !

 

I have to say that i'm a little bit lost with the formats / informats / length, i think that i should take a look on documentation about it.

 

I did what you say but instead creating a new variable (agence1), i use the existant one (agence) and it's working perfectly. I still have a little question to ask : Why my variable agence (the original which i have transformed right now) is in $7.  when i'm clicking on it ?

and not in $5. ?

 

data cim_tset2 ;
set cim_tset1 ;
length agence $5. ; /*my original variable*/
agence = put(input(agence, 5.), z5.);
cle=compress(code_banque!!"_"!!agence);
run ;

A variable that comes in from a dataset will inherit its attributes from there, and once it is defined in the PDV, it cannot change the length. You can "change" the length by moving the length statement up above the set statement. You will get a "truncation" WARNING, though.

The proper method is to play the "create new variable/drop the old/rename the new" game.

 

And you are right about going into the documentation; there is a reason why my Maxim 1 is number one.

Ksharp
Super User
data have;
input x $5.;
want=translate(right(x),'0',' ');
cards;
123
12345
1
;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1398 views
  • 3 likes
  • 4 in conversation