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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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