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

I have a char variable VAR1 with these values

'56'
'152'
'7'

and I need to create a new variable VAR2 that adds some left zeroes till 8 characters like

'00000056'
'00000152'
'00000007'

which function I have to use?

thank you all.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
  length VAR1 $8;
  VAR1='56';output;
  VAR1='152';output;
  VAR1='7';output;
run;

data want;
 set have;
 length want $ 8;
 want=left(var1);
 want=translate(right(want),'0',' ');
run;

View solution in original post

3 REPLIES 3
japelin
Rhodochrosite | Level 12

If Var1 value is only number, you can use z format.

 

data have;
  length VAR1 $8;
  VAR1='56';output;
  VAR1='152';output;
  VAR1='7';output;
run;

data want;
  length VAR2 $8;
  set have;
  VAR2=put(input(VAR1,best.),z8.);
run;

or

use repeat function

data want;
  length VAR2 $8;
  set have;
  VAR2=cats(repeat('0',8-length(VAR1)-1),VAR1);
run;
Ksharp
Super User
data have;
  length VAR1 $8;
  VAR1='56';output;
  VAR1='152';output;
  VAR1='7';output;
run;

data want;
 set have;
 length want $ 8;
 want=left(var1);
 want=translate(right(want),'0',' ');
run;
Zaghini
Fluorite | Level 6

thanks  @Ksharp @japelin   ,

it works!

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
  • 3 replies
  • 1004 views
  • 2 likes
  • 3 in conversation