-2

I want an answer for this.

The input I have is:

ABC123

The output I want is:

123ABC 

How to print the output in this format (i.e. backwards) using Proc SQL?

Tom
  • 47,574
  • 2
  • 16
  • 29
ouilr_
  • 1
  • 1
    @ZaynulAbadinTuhin ... [`proc sql`](http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002294523.htm) is a module within SAS software and so its default dialect is SAS specific, mainly [ANSI-1992 SQL](http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002473705.htm) with available SAS functions. – Parfait Mar 12 '19 at 19:22

4 Answers4

0

Based on the information given and assuming that all your data is in same format you can tweak substr function in proc sql

data have;
value='ABC123';
run;

proc sql;
create table want
as
select value, 
       substr(value,4,4)||substr(value,1,3) as new_value
  from have;
quit;

proc print data=want; run;

The same function can be applied in data step as well.

Rhythm
  • 680
  • 5
  • 9
0

You will probably want to use trim() to deal with the trailing spaces that SAS stores in character variables.

trim(substr(have,4))||substr(have,1,3)
Tom
  • 47,574
  • 2
  • 16
  • 29
0

If you want an algorithm that would work with similar strings of any length (any number of letters followed by any number of digits), I suggest using regular expressions to modify the input string.

outStr = prxChange("s/([A-z]+)([\d]+)/$2$1/", 1, inStr);

You can easily use it within proc sql.

data test1;
  inStr = "ABCdef12345";
run;

proc sql;
  create table test2 as
  select prxChange("s/([A-z]+)([\d]+)/$2$1/", 1, inStr) as outStr
  from test1;
quit;
0

Base SAS contains a function REVERSE that is dedicated to reversing a string, and that can be used both in proc sql and in a datastep. See example in SAS documentation or here:

proc sql;
select Name,
       reverse(Name) as Name_reversed
from sashelp.class
;
quit;

output:

Name    | Name_reversed
--------|--------------
Alfred  | derflA
Alice   | ecilA
Barbara | arabraB

etc.

Vojta F
  • 534
  • 3
  • 17