BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

 

Hi guys

 

 Need help with the repeat function, can someone help pls?

 Here is the sample code, just copy and paste:

 

 I want to feel the new_name variable with the var "name" + "@" deppending on the length of the "name" var to a maximum of 10 characters on the "new_name" var. For example, please see the image RESULT.JPG atteched, and the code below:

 

  • RESULT.jpg:

 

 RESULT.jpg

 

I tried the repeat function but I cannot change the number of repetitions.

 

data have;
input name $;
datalines;
ferrari
mclaren
red bull
renault
mercedes
jordan
minardi
force india
;
run;
data have;
set have;
lentgh_name = length(name);
run;

Thanks and regards

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @anonymous_user,

 

Let's first correct your first data step ...

 

data have;
input name & $11.;
datalines;
ferrari
mclaren
red bull
renault
mercedes
jordan
minardi
force india
;

List input without the "&" modifier would stop reading a value at the first blank (thus truncating 'red bull' to 'red', see your own result). Also, without a length specification (in the above code contained in the informat specification $11.) values would be truncated to 8 characters (e.g. 'force india' to 'force in').

 

I don't know why you couldn't "change the number of repetitions" with the REPEAT function because you didn't post your function call. Please see how I used the function in the code below:

data want;
length new_name $11;
set have;
if length(name)<10 then new_name=cats(name, repeat('@',9-length(name)));
else new_name=name;
length_new_name = length(new_name);
run;

I've always found it a bit counterintuitive that the REPEAT function returns n+1 repetitions of the first argument (without allowing -1 as the second argument), but that's how it is.

 

Please note that the longest name ('force india') has length 11. Maybe you want to increase the number of @ signs by 1? If so, please change '10' and '9' to '11' and '10', respectively.

 

Other options to achieve the same result include the SUBSTR, PRXCHANGE or TRANSLATE function (the latter with care for embedded blanks).

 

 

View solution in original post

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hi @anonymous_user,

 

Let's first correct your first data step ...

 

data have;
input name & $11.;
datalines;
ferrari
mclaren
red bull
renault
mercedes
jordan
minardi
force india
;

List input without the "&" modifier would stop reading a value at the first blank (thus truncating 'red bull' to 'red', see your own result). Also, without a length specification (in the above code contained in the informat specification $11.) values would be truncated to 8 characters (e.g. 'force india' to 'force in').

 

I don't know why you couldn't "change the number of repetitions" with the REPEAT function because you didn't post your function call. Please see how I used the function in the code below:

data want;
length new_name $11;
set have;
if length(name)<10 then new_name=cats(name, repeat('@',9-length(name)));
else new_name=name;
length_new_name = length(new_name);
run;

I've always found it a bit counterintuitive that the REPEAT function returns n+1 repetitions of the first argument (without allowing -1 as the second argument), but that's how it is.

 

Please note that the longest name ('force india') has length 11. Maybe you want to increase the number of @ signs by 1? If so, please change '10' and '9' to '11' and '10', respectively.

 

Other options to achieve the same result include the SUBSTR, PRXCHANGE or TRANSLATE function (the latter with care for embedded blanks).

 

 

stat_sas
Ammonite | Level 13

data have;
input name $15.;
datalines;
ferrari
mclaren
red bull
renault
mercedes
jordan
minardi
force india
;

 

data want(drop=char i);
set have;
length new_name $15.;
char='@';
new_name=name;
do i=length(name)+1 to 10;
new_name=catt(new_name,char);
end;
run;

Reeza
Super User

If your using query builder you can use the repeat function in a calculated column with the same parameters. 

Astounding
PROC Star

If plan on limiting your new varaible to a length of 10, you can do this fairly simply:

 

length new_name $ 10;

new_name = trim(name) || '@@@@@@@@@@';

 

There may not be room int he new variable to store all the "@" characters, so you automatically lose the ones that you don't need.

Thank you all for your help!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2436 views
  • 4 likes
  • 5 in conversation