## how to pad character variables with values

Solved
Occasional Contributor
Posts: 8

# how to pad character variables with values

Hello ,

I have a large dataset where I  need to add leading number to some of the variables. here is a subset of the data

Data  have;

11-000-0067
5511-000-00267
5511-000-00367
11-000-01166
5511-000-11166

I want to add  55 to those that are missing the leading number to get the following result

data want;

5511-000-00671
5511-000-00267
5511-000-00367
5511-000-01166
5511-000-11166

Thanks.

Accepted Solutions
Solution
2 weeks ago
Valued Guide
Posts: 558

## Re: how to pad character variables with values

Did you miss "1" in your first records for the data you have?

``````data have;
input num \$15.;
datalines;
11-000-00671
5511-000-00267
5511-000-00367
11-000-01166
5511-000-11166
;
run;
data want;
set have;
if length(num)=12 then num=cats("55",num);
run;``````
Thanks,
Suryakiran

All Replies
Super User
Posts: 9,402

## Re: how to pad character variables with values

Is it always 55 in front, and 1 at end?  I would do something like:

```data want;
set have;
length want want1 want2 want3 \$50;
if lengthn(scan(have,1,"-")) ne 4 then want1=cats(repeat("5",4-lengthn(scan(have,1,"-")),scan(have,1,"-"));
else want1=scan(have,1,"-");
...
want=catx('-',want1--want3);
run;
```

Ie break the string up into three, check the length, padd as needed, then cat together.

Occasional Contributor
Posts: 8

## Re: how to pad character variables with values

I forgot to mention that some values can be

11-000-abcd1 , so this is not a numeric variable.

Thanks.

Solution
2 weeks ago
Valued Guide
Posts: 558

## Re: how to pad character variables with values

Did you miss "1" in your first records for the data you have?

``````data have;
input num \$15.;
datalines;
11-000-00671
5511-000-00267
5511-000-00367
11-000-01166
5511-000-11166
;
run;
data want;
set have;
if length(num)=12 then num=cats("55",num);
run;``````
Thanks,
Suryakiran
Occasional Contributor
Posts: 8

## Re: how to pad character variables with values

this code does not seem to work for me. Please note this is a character variable,

Thanks.

Valued Guide
Posts: 558

## Re: how to pad character variables with values

Please show what you get in output.

Check if leading or trailing blanks are causing the issue.

``if length(num)=12 then num=cats("55",strip(num));``

Thanks,
Suryakiran
Occasional Contributor
Posts: 8

## Re: how to pad character variables with values

After another try, this works perfectly.

Thank you so much!

Occasional Contributor
Posts: 6

## Re: how to pad character variables with values

``````data have;
input number:\$20.;
cards;
11-000-00671
5511-000-00267
5511-000-00367
11-000-01166
5511-000-11166
;

data want;
set have;
length number2 \$20.;
number2 = tranwrd(put(number, 14.-R),' ','5');
run;``````
Super User
Posts: 6,628

## Re: how to pad character variables with values

Assuming that you have a character variable that might contain exactly two leading blanks:

if (var > ' ') and (var =: '  ') then substr(var, 1, 2) = '55';

The assumptions about what is in the data are critical, however, and this only works if the assumptions are correct.

☑ This topic is solved.