DATA Step, Macro, Functions and more

Get 16 characters of a variable

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

Get 16 characters of a variable

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


Accepted Solutions
Solution
‎01-25-2017 01:46 PM
Super User
Posts: 11,343

Re: Get 16 characters of a variable

Posted in reply to Angel_Saenz

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


All Replies
Solution
‎01-25-2017 01:46 PM
Super User
Posts: 11,343

Re: Get 16 characters of a variable

Posted in reply to Angel_Saenz

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;
Contributor
Posts: 43

Re: Get 16 characters of a variable

var1 and var2 are numeric, butI need var3 char16 because I need to marge with other var (char16)
Super User
Super User
Posts: 7,942

Re: Get 16 characters of a variable

Posted in reply to Angel_Saenz

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.

Contributor
Posts: 43

Re: Get 16 characters of a variable

THANK YOU
Super User
Posts: 11,343

Re: Get 16 characters of a variable

Posted in reply to Angel_Saenz

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 111 views
  • 3 likes
  • 3 in conversation