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!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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