BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
112211
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Daniel_CGB
Fluorite | Level 6

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;

 

View solution in original post

9 REPLIES 9
Daniel_CGB
Fluorite | Level 6

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;

 

mkeintz
Jade | Level 19

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;

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

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.

112211
Obsidian | Level 7
typo mistake
Kurt_Bremser
Super User
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.

ErikLund_Jensen
Rhodochrosite | Level 12

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, 

mkeintz
Jade | Level 19

@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

  1. From Obscurity to Utility: ADDR, PEEK, POKE as DATA Step Programming Tools 
  2. Direct Memory Data Management Using the APP Tools 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
yabwon
Meteorite | Level 14

Using trick from this thread:

https://communities.sas.com/t5/SAS-Programming/How-to-delimit-large-dataset-28-Million-rows-into-700...

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Meteorite | Level 14

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

How to Concatenate Values

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 568 views
  • 3 likes
  • 7 in conversation