Data Have;
Set Some;
Do i=1 to 26 until (i=ORD);
Out=Compbl(Substr(ALPHA,1,1)||''||Substr(ALPHA,1,1));
Output;
end;
run;
It's a simple do loop, with the BYTE function:
data want;
length string $26;
do i = 1 to 26;
char = byte(i + offset);
string = cats(string,char);
output;
end;
run;
You need to replace "offset" with the correct value to start in the right position of the ASCII table.
I Have tried with above code but i couldn't get the output.
It's a simple do loop, with the BYTE function:
data want;
length string $26;
do i = 1 to 26;
char = byte(i + offset);
string = cats(string,char);
output;
end;
run;
You need to replace "offset" with the correct value to start in the right position of the ASCII table.
I explicitly linked to the documentation for you to read. In the same place (navigation pane to the left) you can also find the cats() function. do loop and output are self-explaining.
@Rakesh93 wrote:
Can you describe how byte function and this whole code works??
The best way to answer your question is to search the specific functions
is searching google "sas documentation <function>".
The BYTE function converts the ascci numeric value into a character.
CATS function combines concatenation with compress.
Here as a variation of what @Kurt_Bremser posted. All the functions used are documented here.
The basic idea of the code: Loop over the decimal values from an ASCII (or EBCDIC) table. Start the loop with a as the first letter and end with z as the last letter. If you look into an ASCII table an a has a decimal value of 97, and a z a value of 122. So if we know where to start and end then we can just implement a simple do loop and then use the SAS byte() function to convert the decimal value to it's character representation. Nicely SAS also provides a rank() function which does the opposite to the byte() function: It takes a character and returns its decimal value. So using rank() we can easily determine the start and stop value for a do loop - and then use the values of the do loop counter in the byte() functions to get the characters.
data want;
format dec_val z3.;
length char $1 string $26;
_start=rank('a');
_stop =rank('z');
do dec_val = _start to _stop;
char = byte(dec_val);
string = cats(string,char);
output;
end;
drop _:;
stop;
run;
proc print data=want;
run;
Rank() function may also help:
data want;
length string $26;
do i = rank("A") to rank("Z");
char = byte(i);
string = cats(string,char);
output;
end;
run;
All the best
Bart
Also a good place to use SUBSTR() on the left side of the assignment statement.
data want;
do index=1 to 26 ;
length char $1 string $26 ;
char=byte(rank('a')+index-1);
substr(string,index,1)= char;
output;
end;
run;
proc print;
run;
Obs index char string 1 1 a a 2 2 b ab 3 3 c abc 4 4 d abcd 5 5 e abcde 6 6 f abcdef 7 7 g abcdefg 8 8 h abcdefgh 9 9 i abcdefghi 10 10 j abcdefghij 11 11 k abcdefghijk 12 12 l abcdefghijkl 13 13 m abcdefghijklm 14 14 n abcdefghijklmn 15 15 o abcdefghijklmno 16 16 p abcdefghijklmnop 17 17 q abcdefghijklmnopq 18 18 r abcdefghijklmnopqr 19 19 s abcdefghijklmnopqrs 20 20 t abcdefghijklmnopqrst 21 21 u abcdefghijklmnopqrstu 22 22 v abcdefghijklmnopqrstuv 23 23 w abcdefghijklmnopqrstuvw 24 24 x abcdefghijklmnopqrstuvwx 25 25 y abcdefghijklmnopqrstuvwxy 26 26 z abcdefghijklmnopqrstuvwxyz
If the real goal is to get the string abcd...z then you don't need a loop or the BYTE() function. Just use the COLLATE() function instead.
data want;
length string $26;
string=collate(rank('a'),rank('z'));
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.