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.)
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
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.
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;
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;
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
;
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;
or it should be:
b = put(catt(substr('xxxxxxxxxx',1,10-length(a)),a),$10.);
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!
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.