BookmarkSubscribeRSS Feed
Mike_Davis
Fluorite | Level 6

Hello everyone,

there is a column A whoes format  is  $7, I want create a new column B with $10.  the rule of create B is fill 'x' on the left side of A.

Thanks

Mike

for example:

1.

in column A,a row has value   '12345'

I want to create B with value 'XXXXX12345' ($10.)

2.

in column A,a row has value   '1234567'

I want to create B with value 'XXX1234567'($10.)

7 REPLIES 7
Haikuo
Onyx | Level 15

data have;

input a $7.;

cards;

1234567

1234

12345

;

data want;

set have;

b=cats(repeat('x',9-length(a)),a);

run;

proc print;run;

Haikuo

Tom
Super User Tom
Super User

You could use TRANSLATE (or even TRANWRD) function.

Use the RIGHT function to get the original value moved to the right then replace the spaces with XXX.

data want;

  set have;

  length b $ 10;

  b=a;

  b=translate(right(b),'X',' ');

  put (a b) (= $quote.);

run;


a="1234567" b="XXX1234567"

a="1234" b="XXXXXX1234"

a="12345" b="XXXXX12345"

Note that it is the LENGTH of the character variable that is important, not the FORMAT that is attached to it.

Linlin
Lapis Lazuli | Level 10

borrowed Astounding's code:

data have;

a='1234567';

output;

a='123';

output;

run;

data want;

length b $10;

set have;

b='xxxxxxxxxx';

substr(b, 10-length(a)+1) = a;

proc print;

run;

MikeZdeb
Rhodochrosite | Level 12

hi ... the more the merrier ...

proc format;

picture a low-high='0000000000' (fill='X');

run;


data want;

set have;

b = put(input(a,7.),a.);

run;

Mike_Davis
Fluorite | Level 6

Thank you Mike

If we have dataset like this:(not numbers but characters),how to do it in this way?

Thanks!

Mike

data have;

input a $7.;

cards;

aaaaaa

bbbbb

cccc

;

MikeZdeb
Rhodochrosite | Level 12

hi ... PICTURE formats work with numeric variables

there have been several nice solutions posted that work with character data, here's another possibility ...

data want;

length b $10;

set have;

b =catt(a,substr('xxxxxxxxxx',1,9-length(a)));

run;


similar to Haikuo 's solution, but you should use a length statement when using the CAT functions given that the default length of the result is 200


if you really wanted a one statement solution that gives you a variable with a length of 10, you could get rid of the LENGTH statement and use a PUT function with the CATT result ...


data want;

set have;

b = put(catt(a,substr('xxxxxxxxxx',1,9-length(a))),$10.);

run;


Linlin
Lapis Lazuli | Level 10

or it should be:

b = put(catt(substr('xxxxxxxxxx',1,10-length(a)),a),$10.);

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 2401 views
  • 6 likes
  • 5 in conversation