BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
112211
Obsidian | Level 7
Data do;
A= 'United States of America ';
Run ;

I need output like reverse words below mentioned (preferably use do loops 😞
America of states united
1 ACCEPTED SOLUTION

Accepted Solutions
maguiremq
SAS Super FREQ

Hi @112211, I didn't see your desired output until now. 

 

data want (drop = i);
length a_want $50.; /* Needs to be set depending on output */
a = "United States of America";
do i = countw(a, " ") to 1 by -1;
a_want = catx(" ", a_want, scan(a, i, " "));
end;
run;

Basically, your loop starts at 4 because COUNTW finds four words. I use the by -1 to go from 4 --> 3 --> 2 -- 1.

 

CATX then combines each word with the DO loop.

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Use the REVERSE function, no loops needed

 

data want;
    set do;
    reverse_A=reverse(A);
run;

 

--
Paige Miller
maguiremq
SAS Super FREQ

Hi @112211, I didn't see your desired output until now. 

 

data want (drop = i);
length a_want $50.; /* Needs to be set depending on output */
a = "United States of America";
do i = countw(a, " ") to 1 by -1;
a_want = catx(" ", a_want, scan(a, i, " "));
end;
run;

Basically, your loop starts at 4 because COUNTW finds four words. I use the by -1 to go from 4 --> 3 --> 2 -- 1.

 

CATX then combines each word with the DO loop.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 580 views
  • 2 likes
  • 3 in conversation