Q: SAS SQL or data step: How to select values that begin with different numbers Maybe the title is a bit blurry, I hope you will understand my question.
Background: I work with data about university students, and I have been given the task of calculating the grade point averages for all exams on a specific semester for some subset of predefined students. The time span of the data is 10 years and activity names for the exams varies across time. 
My problem: Data has a hierarchy structure for the level of education, semester and activity names. How can I use a data step/SQL on the lowest level to select only those values beginning with value for the top hierarchy. 
The hierarchy is the following (for a single student):
13109963 
1310996300001
131099630000100001
13109963000010000100001 (activities)
13109963000010000100002 (activities)
13109963000010000100003 (activities)

I have struggled with this for a few hours, and would appreciate suggestions how to proceed on this special problem. I have been using SAS for three years and have a background from the social sciences.
Thank you in advance
Henning

A: There are a few options in SAS that will let you accomplish this task.
Here is a quick example.
 data sample;
length  level $60.;
input  level $1-60;
/*Option 1
 Subset obs based on top hierarchy and select bottom hierarchy
 based on the length of the string
*/
opt1 = substr(level,1,8)='13109963' and length(level)>23 ;
/*Option 2
 Subset obs based on the length of the string to pick all lowest level regardless
 of the top hierarchy (in this case will return the same result)
*/
opt2 = length(level)>23 ;
/* Option 3:
 if all lowest level  hierarchies have a '(' you can then use the index function
*/
opt3 = index(level,'(')>1;
/* Option 4: Is using regex but I think any of the above will suffice*/
cards;
13109963 
1310996300001
131099630000100001
13109963000010000100001 (activities)
13109963000010000100002 (activities)
13109963000010000100003 (activities) 
;
run;

You can also mix&match any of the options above to select only those observations required.
For example: if substr(level,1,8)='13109963'and index(level,'(')>1; will return the same result.
