data name;
input a $ score;
cards;
virat 100
Charles 500
william 250
Sachin 150
warwick 100
roger 120
taylor 152
jhonny 165
hunter 125
jackson 156
;
from using above data i need output should be following
name
v*i*r*a*t 100
c*h*a*r*l*e*s 500
w*i*l*l*i*a*m 250
s*a*c*h*i*n 150
w*a*r*w*i*c*k 100
r*o*g*e*r 200
t*a*y*l*o*r 152
j*h*o*n*n*y 165
h*u*n*t*e*r 125
j*a*c*k*s*o*n 156
The below datastep code can achieve what you want.
data name1;
length a_new $ 30;
set name;
do ii=1 to length(a);
if ii<length(a) then a_new=trim(a_new)||substr(a,ii,1)||"*";
else a_new=trim(a_new)||substr(a,ii,1);
end;
drop ii;
run;
The below datastep code can achieve what you want.
data name1;
length a_new $ 30;
set name;
do ii=1 to length(a);
if ii<length(a) then a_new=trim(a_new)||substr(a,ii,1)||"*";
else a_new=trim(a_new)||substr(a,ii,1);
end;
drop ii;
run;
You could copy each character of variable A into an array, then apply the CATX() function to the array to produce the new variable, with the '*' as a letter-separator.
data names;
input a $ score;
cards;
virat 100
Charles 500
william 250
Sachin 150
warwick 100
roger 120
taylor 152
jhonny 165
hunter 125
jackson 156
;
data want (drop=_:);
set names;
array _letter {8} $1;
length anew $15 ;
do _n_=1 to length(a);
_letter{_n_}=char(a,_n_);
end;
anew=catx('*',of _letter{*});
run;
Was the change in case for Charles and Sachin (the only two in the example that start with capital letters) to c*h*a*r*l*e*s and s*a*c*h*i*n , now with lower case, actually intentional or just typos?
Note your example Name variable with have a length of 8 characters. None of those resulting values will fit in 8 characters so you need to create a new variable to holder the longer values.
proc sql;
select (2 * length) into :l from dictionary.columns
where libname = "WORK" snd memname = "NAME" and upcase(name) = "A";
quit;
data want;
length a $&l.;
set name (rename=(a=_a)),
do i = 1 to length(_a);
a = catx("*",a,substr(_a,i,1));
end;
drop i _a;
run;
Edit: forgot to add the multiplication in the first step. Corrected now.
Hi @112211
A different way of doing the same as @Kurt Bremsers solution. There is nothing wrong with that, so this is just made to show that there are many ways in SAS to achieve the same result.
data names;
input a $ score;
cards;
virat 100
Charles 500
william 250
Sachin 150
warwick 100
roger 120
taylor 152
jhonny 165
hunter 125
jackson 156
;
run;
data _null_;
set names (obs=1);
call symputx('len',vlength(a)*2);
run;
data want;
length a $&len;
set names;
a = substr(prxchange('s//*/',-1,a),2,(length(a)*2)-1);
run;
The data _null_ step computes the length of the output variable by doubling the length of the input variable and storing the result in the macro variable len.
The macro variable is used in the next step before the set statement to change the length of variable a. The prxchange function changes all occurrences of "nothing" to *, and the substring removes the resulting leading and trailing * to create the wanted output. The prxchange function can do things that makes a loop superflouos, but I don't think that makes it faster or more readable,
@ErikLund_Jensen wrote:
Hi @112211
A different way of doing the same as @Kurt Bremsers solution. There is nothing wrong with that, so this is just made to show that there are many ways in SAS to achieve the same result.
Yup.
And if one wants not only to avoid any DO loop, but also generate discussion, you can utilize APP (address/peek/poke) functions:
data names;
input a $ score;
cards;
virat 100
Charles 500
william 250
Sachin 150
warwick 100
roger 120
taylor 152
jhonny 165
hunter 125
jackson 156
run;
%let origvarlength=8;
data want (drop=_:);
set names;
array _letter {&origvarlength} $1;
length new_a $%eval(2*&origvarlength -1) ;
call pokelong(a,addrlong(_letter1));
new_a=catx('*',of _letter{*});
run;
But the APP functions are effectively being deprecated by SAS, as they are not available in the heavily-promoted CAS.
I am not recommending using the above, but for the curious, you can learn more in these papers, both by Paul Dorfman
Using trick from this thread:
proposed by @Tom and summarised by @AhmedAl_Attar you can do it like this:
data names;
input a :$20. score;
cards;
virat 100
Charles 500
william 250
Sachin 150
warwick 100
roger 120
taylor 152
jhonny 165
hunter 125
jackson 156
;
filename d TEMP;
data _null_;
file d;
put;
run;
data want (drop=L:);
set names;
infile d;
input @1 @;
_infile_ = a;
input @1 (L1-L20) ($1.) @@; /* 20 - length of a or less */
anew=catx('*',of L:);
run;
All the best
Bart
BTW. If you decide to work with multilanguage data (in multi byte encoding like UTF-8) you should probably start using the K...() functions:
data names;
input a :$20. score;
cards;
ŻÓŁĆ 123
空手道 456
;
data want (drop=i);
set names;
length aNew $ 200;
do i = 1 to klength(a);
aNew=catx('*', aNew, ksubstr(a,i,1));
end;
run;
Bart
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.