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

As an exercise, I am trying to disassemble a sting into individual characters, then reassemble the characters back to the original string into a new SAS dataset.

 

I can successfully disassemble the sting with this code:

data looky (drop=string);
input string $ 1-50;
do i=1 to length(string);
letter = substr(string, i, 1);
output;
end;
cards;
The quick brown fox jumped over the lazy dogs
;
run;

 

However, when I attempt to reassemble the string, the blanks are removed.  I've tried using TRIM, STRIP, CAT, CATT and CATS and they all remove blanks:

data what;
length string $ 50;
retain string;
set work.looky;
string=strip(string)||letter;
run;

STRIP (and the other functions) returns Thequickbrownfoxjumpedoverthelazydogs

 

This simple put statement write the complete string to the log, blanks included:

data _null_;
set work.looky;
put @i letter @@; run;

The quick brown fox jumped over the lazy dogs

 

I am obviously misapplying the TRIM, STRIP CATT functions, but I am truly baffled as to what to use that will retain blanks.  Simply using string||letter returns a completely blank column.  What am I doing wrong?

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @MarkFisher,

 


@MarkFisher wrote:

letter = substr(string, i, 1);


You could turn around the above assignment statement to reassemble the characters:

data want;
length string $ 50;
retain string;
set work.looky;
substr(string, i, 1) = letter;
run;

View solution in original post

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hello @MarkFisher,

 


@MarkFisher wrote:

letter = substr(string, i, 1);


You could turn around the above assignment statement to reassemble the characters:

data want;
length string $ 50;
retain string;
set work.looky;
substr(string, i, 1) = letter;
run;
Tom
Super User Tom
Super User

SAS stores character variables as fixed length. So when you define STRING as length $50 it contains 50 spaces. When you set it to 'T' it now contains the letter T and 49 spaces.

 

You need to know where in those 50 characters you want to place the letter.

You could just write directly to the i'th position.

substr(string,i,1)=letter;

Or extract the first (i-1) values and append the new letter.

string=substrn(string,1,i-1)||letter;
JosvanderVelden
SAS Super FREQ
By using strip(string) in the line "string=strip(string)||letter;" you strip all leading and trailing blanks. That does not happen in the code from the reply of FreelanceReinhard. You could also use something like "string = substr(string, 1, _N_) || letter;" in stead of the line mentioned above..
MarkFisher
Calcite | Level 5

Goodness, I didn't expect the solution to be so simple!  I was obviously mis-applying the TRIM, STRIP and CAT functions.

 

Thank you all for your timely and thoughtful replies!

MarkFisher
Calcite | Level 5
Oddly enough, this statement writes the string in reverse order, with blanks included:
string=trim(left(letter))||string;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 5 replies
  • 1003 views
  • 4 likes
  • 4 in conversation