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

How can I get 16 characters of a variable starting from right and fill blank spaces with zeros?, example:

 

Var 1 Minimum has 6 characters and is incremented

Var 2 Always has 3 characters

 

Var1         Var2

123456     100

1234567   100

12345678 100

 

I want to have result

var3

0000000123456100

0000001234567100

0000012345678100

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Is Var1 character or numeric?

 

Here's one way. Pick the appropriate for the var1 variable type:

data example;
   Var1Num= 123456;
   var2 = 100;
   var1char = '123456';
   var3num = cats(put(var1num,z13.),var2);
   var3char= cats(put(input(var1char,best16.),z13.),var2);
run;

View solution in original post

5 REPLIES 5
ballardw
Super User

Is Var1 character or numeric?

 

Here's one way. Pick the appropriate for the var1 variable type:

data example;
   Var1Num= 123456;
   var2 = 100;
   var1char = '123456';
   var3num = cats(put(var1num,z13.),var2);
   var3char= cats(put(input(var1char,best16.),z13.),var2);
run;
Angel_Saenz
Quartz | Level 8
var1 and var2 are numeric, butI need var3 char16 because I need to marge with other var (char16)
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Something like:

data want;
  set have;
  length var3 $16;
  var3=cats(put(var1,z13.),put(var2,z3.));
run;

Putting a value into a Zx. format padds out the number with '0' characters to thelength specified.

Angel_Saenz
Quartz | Level 8
THANK YOU
ballardw
Super User

@Angel_Saenz wrote:
var1 and var2 are numeric, butI need var3 char16 because I need to marge with other var (char16)

Set the Length for the desired variable before assigning the value:

 

Length var3 $ 16.;

That will set the length instead of using the default coming from the CATS function.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 1665 views
  • 3 likes
  • 3 in conversation