Help using Base SAS procedures

how to create variable like this?

Reply
Regular Contributor
Posts: 222

how to create variable like this?

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.)

Respected Advisor
Posts: 3,124

Re: how to create variable like this?

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

Super User
Super User
Posts: 6,498

Re: how to create variable like this?

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.

Super Contributor
Posts: 1,636

Re: how to create variable like this?

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;

Valued Guide
Posts: 765

Re: how to create variable like this?

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;

Regular Contributor
Posts: 222

Re: how to create variable like this?

Thank you Mike

If we have dataset like thisSmiley Sadnot numbers but characters),how to do it in this way?

Thanks!

Mike

data have;

input a $7.;

cards;

aaaaaa

bbbbb

cccc

;

Valued Guide
Posts: 765

Re: how to create variable like this?

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;


Super Contributor
Posts: 1,636

Re: how to create variable like this?

or it should be:

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

Ask a Question
Discussion stats
  • 7 replies
  • 222 views
  • 6 likes
  • 5 in conversation