Encep Taufik
Encep Taufik

Reputation: 1

Rearrange text on SAS

I can not find the way to reverse text strings.

For example I want to reverse these:

Upvotes: 0

Views: 52

Answers (1)

user2877959
user2877959

Reputation: 1792

Judging from your examples, what you mean by 'rearrange' is actually 'reverse'. In that case, you've got the very handy reverse() function in SAS.

Used in context:

data test;
  length text $32;
  infile datalines;
  input text $;
  result=reverse(strip(text));
datalines;
MMMM121231M34
MM1M11M1
1111213111
;
run;

EDIT on @Joe's request: in the particular example above, I create the test dataset by setting a length of 32 characters for the text variable. Therefore, when reading the values from datalines, these are padded with blanks up to that total of 32 characters. Hence, when reversing that value, the result has that many blanks at the start, followed by the actual value you are looking for. By adding the strip function, you remove the excess blanks from the value of text before reversing, keeping only the "real" value in the result.

Upvotes: 1

Related Questions