BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASbeginner4
Fluorite | Level 6

Hello to everyone; please help me to write a program to Take n’th character from n’th word and keep in separate variables using arrays.

Take n’th character from n’th word and keep in separate variables

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So if the source variable has a length of 200 you will need 200 new one character variables.

data want;
  set have;
  array c [200] $1 ;
  do i=1 to lengthn(string);
     c[i]=char(string,i);
  end;
run;

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Show us what you have tried so far.

 

Also, I'm not really clear what that means: "Take n’th character from n’th word and keep in separate variables". Can you give examples?

 

And why do we have to do this with arrays?

--
Paige Miller
SASbeginner4
Fluorite | Level 6

There is a string, for example, "I had been caught going through a traffic light on the change." and there is necessary to keep every letter as a variable, for example var1 var2 var3 var4 and so on till the end.

                                                                                                             I         h       a       d                                                           

 

PaigeMiller
Diamond | Level 26

Why? I can't really see a point to taking a text string and separating it into individual letters.

 

And I ask again: show us what you have tried.

--
Paige Miller
Tom
Super User Tom
Super User

So if the source variable has a length of 200 you will need 200 new one character variables.

data want;
  set have;
  array c [200] $1 ;
  do i=1 to lengthn(string);
     c[i]=char(string,i);
  end;
run;
SASbeginner4
Fluorite | Level 6

Thank you very much, it works

ballardw
Super User

@SASbeginner4 wrote:

There is a string, for example, "I had been caught going through a traffic light on the change." and there is necessary to keep every letter as a variable, for example var1 var2 var3 var4 and so on till the end.

                                                                                                             I         h       a       d                                                           

 


What will you do after you have this likely to be obnoxious number of variables?

SAS INNOVATE 2024

innovate-wordmarks-white-horiz.png

SAS is headed back to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team.

Interested in speaking? Content from our attendees is one of the reasons that makes SAS Innovate such a special event!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 6 replies
  • 217 views
  • 2 likes
  • 4 in conversation