Skip to content Skip to sidebar Skip to footer

How Do I Read a String Separated by Space in Sas

A jargon-free, piece of cake-to-learn SAS base class that is tailor-made for students with no prior knowledge of SAS.

The Scan part in SAS

The SCAN function in SAS provides a simple and user-friendly way to parse out words from character strings. The Browse role tin exist used to select individual words from text or variables which comprise text and then store those words in new variables. This article provides a number of different examples and uses for the SCAN role, including some of the virtually commonly used options to help you lot get the nigh from this function.

 In particular, this article will embrace:

  1. Selecting the nth word in a character string.
  2. Selecting the concluding give-and-take in a graphic symbol string.
  3. Treatment different word delimiters.
  4. Using Browse with DO LOOPs to parse long character strings.

Software

Before we keep, make sure yous have access to SAS Studio. It's free!

Information Sets

In this article, the CARS and BASEBALL datasets from the SASHELP library volition exist used to illustrate a number of different uses for the SCAN function.

Selecting the Nth Discussion in a Character String

One of the simplest operations you can perform with the SCAN part is to observe the nth give-and-take in a character string.

 Let's first with an case to demonstrate how to find the get-go word in a character cord and so shop the event in a separate variable. The about basic use of the Scan role requires only ii arguments. After specifying Browse and an open parenthesis, the starting time office of the function is to specify the character string that you are planning to select words from. This can be either a variable or an explicit character string. In this first case nosotros are using the explicit character string, "I am an Expert SAS Developer".

 The 2nd argument is the count, which is the numeric position of the word within the character string that you want to search. So, to return the first discussion, we can explicitly specify a number ane. This could also exist replaced with a variable containing the desired count value.

 The SAS syntax is as follows:

data example;
 first_word = scan( "I am a SAS Programming Expert",i );
run;

Equally you can see in the output below, the new variable FIRST_WORD has been created and its value is the get-go word, "I" from the character string, "I am a SAS Programming Expert":

In most cases, the character string that yous would like to select words from is independent in a variable itself. In this adjacent example, the variable TEXT contains the character string "I am a SAS Programming Adept" and again nosotros would like to extract the first word from the string. To do this, we simply replace the explicit character string in quotes with the variable name TEXT. The count, 1, stays the same as the previous example since we are nonetheless interested in the kickoff discussion:

data example;
 text = "I am a SAS Programming Expert";
 first_word = scan( text ,ane);
run;

The output dataset now contains both the original TEXT variable and the newly created FIRST_WORD variable which contains the first word from the TEXT variable, "I":

To select additional words, such as the second, 3rd and fourth give-and-take, we can modify the count argument of the SCAN function. To select the second word from a string, simply ready the count statement to two. For the tertiary give-and-take, set the count equal to iii, and and so on.

 In the following example, we create 3 additional variables, SECOND_WORD, THIRD_WORD and FOURTH_WORD, which select the second, 3rd and fourth word respectively from the TEXT variable:

data example;
 text = "I am a SAS programming skilful";
 first_word = scan(text,1);
 second_word = scan(text,2);
 third_word = browse(text,iii);
 fourth_word = scan(text,4);

run;

The output data now has 5 variables – the original TEXT variable as well as the first through fourth word, each in separate variables, as shown hither:

Do y'all have a hard time learning SAS?

Take our Practical SAS Training Course for Absolute Beginners and learn how to write your showtime SAS program!

Selecting the Terminal Word in a Character String

Using the SCAN function, you also the have the ability to read from correct to left, finer allowing yous to capture the concluding word in a character string.

 To tell SAS to read from right to left, we but change the count argument to be a negative number to indicate the word number that nosotros would similar to read, starting from the right and moving left. So, to select the discussion "Skillful" in our TEXT variable, we tin utilise a count of -1, as shown hither:

data example;
 text = "I am a SAS Programming Practiced";
 last_word =scan(text,-1);
run;

As you lot can see in the output data, nosotros now have a new variable, LAST_WORD, which contains the last word of the text string, "Adept":

Alternatively, instead of using a negative count you tin use the "b" modifier bachelor with the Browse function. By specifying a "b" argument with the Browse function, you can tell SAS to read from right to left instead of the default left to right. Notation when using a modifier with the Scan role, the modifier needs to be the fourth argument, then y'all must always explicitly country the third argument (the delimiter) together with the fourth modifier argument so that SAS won't care for your modifier as the delimiter!

Here is the syntax with the "b" modifier included:

data instance;
 text = "I am a SAS Programming Good";
 last_word = browse(text,1," ", "b" );
run;

The resulting output dataset shows the same result as previously – the last word, "Expert" has been captured in the LAST_WORD variable:

Notation at that place are many other modifiers for the Browse function to help with special cases. These modifiers can be constitute in the SAS documentation.

Get a Certified SAS Specialist

Go access to two SAS base certification prep courses and 150+ exercise exercises

Handling Different Word Delimiters

So far, the examples we have looked at accept only had blanks or spaces as the delimiter between words. What happens when in that location is a unlike delimiter, such as a comma?

 In the example below, the code has been modified and so that the words in the character string of the text variable are delimited with a comma instead of spaces. Hither, nosotros are trying to select the fourth discussion:

data example;
 text ="I,am,a,SAS,Programming,Expert";
 fourth_word = scan(text,four);
run;

Equally you can see from the output information shown below, the Browse function still works fifty-fifty with commas as the delimiter:

The reason this still works is considering past default, with any computer using ASCII characters, the Scan part will automatically check for any of the post-obit characters every bit delimiters:

blank ! $ % & ( ) * + , - . / ; < ^ :

 When your data contains a delimiter between words not found in the default listing, you can utilise thecharlist argument (the third argument) with the SCAN function to specify your own custom delimiter.

 For case, if the words in your character cord are delimited with a plus sign (+), you just need to enclose the plus sign in quotations as the 3rd argument to the scan office.

 The syntax below demonstrates how to select the fifth word from a plus sign delimited character string:

data case;
 text ="I+am+a+SAS+Programming+Expert";
 fifth_word = browse(text,5,"+");
run;

In the output data below, you can run into the fifth word in the string has been successfully selected:

In some cases, you lot may likewise desire to forcefulness SAS to utilize but one of the default delimiters. By default, SAS will utilise not just one simply all of the delimiters in the default list. This can get problematic in certain cases when your data contains multiple delimiters.

 In the SASHELP.Baseball dataset, the NAME variable contains a list of first, terminal and center names. The structure is as follows: <terminal proper name>,<firstname><blank><middlename>. Y'all would like to create 2 new variables: LASTNAME and GIVEN_NAMES.

 Since commas and spaces are default delimiters, we start without specifying our own delimiter:

information baseball;
 gear up sashelp.baseball;

 lastname = scan(name,1);
 given_names = browse(name,two);

  keep name given_names lastname;
run;

At offset glance it may appear every bit though the results are right, but after further inspection you will discover that some names were non parsed properly. For example, Andy Van Slyke's given name should take been "Andy" and not "Slyke" as shown below:

To correct this, we can tell SAS to only utilize the comma as a delimiter so that "Van Slyke" will become the terminal name and Andy will be the given name:

information baseball;
 gear up sashelp.baseball game;

  lastname = scan(name,1, "," );
 given_names = scan(name,ii, "," );

  go on name given_names lastname;
run;

Now that the blanks are no longer considered delimiters and but the commas are, we get the desired result in our output data with "Andy" now in the GIVEN_NAMES variable and "Van Slyke" in the LASTNAME variable:

Using Browse with Exercise LOOPS to Parse Long Character Strings

When combined with a simple Do LOOP and a SAS , the Scan office makes information technology easy to parse out each word from a character string into divide variables.

For example, in the SASHELP.CARS dataset, yous would like to parse out each word from the MODEL variable into 5 split up variables. Since the words of the full model name are delimited by spaces, no modification is needed to the delimiter argument and the default can be used.

The code below uses a DO LOOP to scan the MODEL variable and then create the variables MODELNAME1 to MODELNAME5:

data cars_parse;
 gear up sashelp.cars;

  array modelname[5] $fifteen model1-model5;
 practise i = one to 5;
  modelname[i] = browse(model,i,", ");
 end;

  keep model model1-model5;
run;

As you can run into in the output data shown partially below, we now have 5 new MODEL variables, with one discussion per variable:

Chief SAS in 30 Days

Inline Feedbacks

View all comments

susi

scan at a attempt two names is not possible

iconmail

Get latest articles from SASCrunch

SAS Base Certification Exam Prep Course

Two Certificate Prep Courses and 300+ Exercise Exercises

johnsonfroo1991.blogspot.com

Source: https://sascrunch.com/scan-function/

Post a Comment for "How Do I Read a String Separated by Space in Sas"