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

what function can i use to replace the variable with 4  in the place of 4 or multiples of 4 ??

the input  is  

 

data one ;
a='abcdefghijklmnopqrstuvwx';
run; 

 

the output i want is :

 

 abc4efg4hij4lmn4pqr4tuv4

1 ACCEPTED SOLUTION

Accepted Solutions
DanielLangley
Quartz | Level 8

I think you made a mistake in your wanted output.

You put abc4efg4hij4lmn4pqr4tuv4 when I believe it is abc4efg4ijk4mno4qrs4uvw4. The problem being that h.

 

Try this.

 

data work.want;
attrib 	a format = $50.
		want format = $50.
;


a ='abcdefghijklmnopqrstuvwx';
array lettersInA[26] $ _TEMPORARY_;


do i = 1 to lengthn(a);
	if mod(i,4) = 0 then lettersInA[i] = "4";
	else lettersInA[i] = substr(a,i,1);
end;

want = cats(of lettersInA[*]);
drop i;
run; 

View solution in original post

4 REPLIES 4
error_prone
Barite | Level 11
Can't test the code now, try something like:

data work.want;
a ='abcdefghijklmnopqrstuvwx';

do i = 1 to lengthen(a);
if int(i/4) = 0 then do;
substr(a, i, 1) = '4';
end;
end;
run;
DanielLangley
Quartz | Level 8

I think you made a mistake in your wanted output.

You put abc4efg4hij4lmn4pqr4tuv4 when I believe it is abc4efg4ijk4mno4qrs4uvw4. The problem being that h.

 

Try this.

 

data work.want;
attrib 	a format = $50.
		want format = $50.
;


a ='abcdefghijklmnopqrstuvwx';
array lettersInA[26] $ _TEMPORARY_;


do i = 1 to lengthn(a);
	if mod(i,4) = 0 then lettersInA[i] = "4";
	else lettersInA[i] = substr(a,i,1);
end;

want = cats(of lettersInA[*]);
drop i;
run; 
Ksharp
Super User
data _null_;
a ='abcdefghijklmnopqrstuvwx';
put a=;
do i = 4 to length(a) by 4;
substr(a, i, 1) = '4';
end;
put a=;
run;
Aayushi_17
Quartz | Level 8

thank you so much this worked 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1570 views
  • 3 likes
  • 4 in conversation