Desktop productivity for business analysts and programmers

Repeat function with different length

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

Repeat function with different length

 

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

 


Accepted Solutions
Solution
‎04-20-2016 02:50 PM
Trusted Advisor
Posts: 1,116

Re: Repeat function with different length

Hi @hy86,

 

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


All Replies
Solution
‎04-20-2016 02:50 PM
Trusted Advisor
Posts: 1,116

Re: Repeat function with different length

Hi @hy86,

 

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

 

 

Trusted Advisor
Posts: 1,228

Re: Repeat function with different length

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;

Super User
Posts: 19,156

Re: Repeat function with different length

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

Super User
Posts: 5,365

Re: Repeat function with different length

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.

Occasional Contributor
Posts: 16

Re: Repeat function with different length

Thank you all for your help!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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