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