BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hugo_B
Obsidian | Level 7

Hello everyone,

 

I know this subject has been heavily present on various forums, but my case is a little bit special:

 

I have a character variable that represents the position of an element in a tree structure.

Here are some possible values:

 

1

1.1

1.1.1

1.2

...

1.2.12

1.3.a.3

1.3.3.a.2.2

1.3.3.b.1

 

"1" represents the root.

Imagine that 1.2 is a node and 1.2.1 & 1.2.2 represent the two branches/paths. A letter in a the string like in "1.1.a.1" & "1.1.b.1" represents a choice between path a or b.

 

My question is : How to retrieve the parent node from the branches ?

 

For example, I want to:

retrieve "1.3.3" from the string "1.3.3.b.11"

retrieve "1.3" from "1.3.a.3"

or retrieve 1.2.3 from 1.2.3.5

 

I tried several methods and I managed to get the last word of the string and its position using SCAN Function and defining '.' and letters as delimiters ... but how to remove the last word + the possible '.' or letters trailing. Without removing the ones that are inside the string ?

 

I am thinking about retrieving the length position of the last-1 word of the string and remove erveything after this point, but I didn't find the good method yet.

 

I hope this is clear. Thank you in advance for your help.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Hugo_B
Obsidian | Level 7

I managed to find this solution:

 

CALL SCAN( string, -2, last_pos, last_length, '.', 'l')

want = substrn( string, 1, last_pos + last_length -1)

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Use the ANYALPHA function to find the first occurrence of a letter, then take all text to the left.

--
Paige Miller
Hugo_B
Obsidian | Level 7

Hello thank you for your anwser,

 

It wasn't very clear in the message (I edited it) but it's not only letters:

 

I want :

"1.2" from "1.2.4"

"1.2" from "1.2.b.3"

"1.3.a.3" from "1.3.a.3.b.4"

 

Letters can appear several times if the tree is tall.

PaigeMiller
Diamond | Level 26

As far as I can see, you have not stated the actual rule to use for the case 

 

"1.2" from "1.2.4"

 

What is the rule you are using here?

 

With regards to multiple letters, what is the actual rule being used here?

 

"1.3.a.3" from "1.3.a.3.b.4"

And what is the rule used here?

 

retrieve "1.3.3" from the string "1.3.3.b.11"

 

Is there a description of the entire process so that we don't have to generalize from a few examples?

--
Paige Miller
Hugo_B
Obsidian | Level 7

After your answer, I edited the first message to add the case "1.2" from "1.2.4".
The rule is:
Remove the last number before the last "." and if it exists the letter just before the last number and the "." just before the letter.
I am thinking about retrieve the position & length of last-1 word saying that letters and "." are delimiters and remove every character that is at le right of the position+length.

Hugo_B
Obsidian | Level 7

I managed to find this solution:

 

CALL SCAN( string, -2, last_pos, last_length, '.', 'l')

want = substrn( string, 1, last_pos + last_length -1)

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 2241 views
  • 0 likes
  • 2 in conversation