SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Jianan_luna
Obsidian | Level 7

I am currently taking Programming 2, lesson 3, level 2 practice. I cannot make sense the argument: park=substr(parkname,1,find(parkname, 'NP')-2); I make sense the substr argument is to read value from the first character in parkname column. Find argument is to find 'NP' string. But what does -2 stand for here? The third argument in substr is about how long the character we want to subtract. Why we use find argument here? Thanks so much!

Jianan_luna_0-1595350248336.pngJianan_luna_1-1595350267157.png

 

3 REPLIES 3
Reeza
Super User
You'd have to share the actual values of ParkName.

But in general it seems like it's taking a portion of the string. Check what the function FIND returns. The -2 means it reduces the length of the string extracted by 2, so it takes it from the start of the string to 2 characters before the start of the NP string. We can't see the string so I can't explain why that's happening. Maybe there's some extra spaces being trimmed.
smantha
Lapis Lazuli | Level 10

find function finds the position of a string within a main string. Eg

w=find('SAS','A');

returns 2 to be the value of the i.e. location of 'A'. In your example you are getting all the string before 'NP'. Need to look at an example of why the subtraction of -2 is happening.

"The FIND function searches string for the first occurrence of the specified substring, and returns the position of that substring. If the substring is not found in string, FIND returns a value of 0."

 

ballardw
Super User

Please consider the following possible value of Parkname

Yellowstone NP
            ^
            13
          ^
          11

Pasting into a box opened on the forum with </> uses a fixed width font so it is easy to see where characters are by count.

The Find for 'NP' will return 13, the value under the first ^ character.

Note that the end of Yellowstone, the second ^ character is in column 11. So to get to the last position (or character or column depending on how you want to think of it) you subtract 2 from 13 to get 11. We suspect that 2 is the correct value because most times we place a single character between words. Note that this will also work if the character between the NP and the ending "E" of Yellowstone is any single character.

 

This is very common sort of parsing of words. We might not use the simpler Scan function that would work for Yellowstone because of names like "Monument Valley" "Crater Lake" or "Black Canyon of the Gunnison" to name some National Parks in the US.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 884 views
  • 0 likes
  • 4 in conversation