DATA Step, Macro, Functions and more

Reverse Strings

Accepted Solution Solved
Reply
Contributor
Posts: 48
Accepted Solution

Reverse Strings

Hi,

 

Does anybody know a quick way to reverse strings. I basically have some strings like the below;

 

Germany; Munich

 

and I want to change them to

 

Munich; Germany

 

Would you know a good function to use to do this?

 

Thanks

Chris


Accepted Solutions
Solution
Wednesday
Super User
Super User
Posts: 9,193

Re: Reverse Strings

Well, that's not really reversed, just swapped over.  You could do:

want=catx(';',scan(have,2,";"),scan(have,1,";"));

I suspect your issue is more complicated than that.  I would split the string up into rows of data with one element per row.  Then have a look up merge, for country, and one for city, merge that on to get a flag for wether it is a country or city, then transpose it up.  Provide some test data in the form of a datastep and can show code.

View solution in original post


All Replies
Solution
Wednesday
Super User
Super User
Posts: 9,193

Re: Reverse Strings

Well, that's not really reversed, just swapped over.  You could do:

want=catx(';',scan(have,2,";"),scan(have,1,";"));

I suspect your issue is more complicated than that.  I would split the string up into rows of data with one element per row.  Then have a look up merge, for country, and one for city, merge that on to get a flag for wether it is a country or city, then transpose it up.  Provide some test data in the form of a datastep and can show code.

Contributor
Posts: 48

Re: Reverse Strings

Thanks a lot for your advice

Frequent Contributor
Posts: 113

Re: Reverse Strings

data x;
  a='Munich; Germany';
  b=catt(scan(a,2,";"),"; ",scan(a,1,";"));
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 76 views
  • 0 likes
  • 3 in conversation