- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have fields which contain either "String1" or they contain "String1\String2" with "\" as the delimiter between the two strings.
I input the field with INPUT @col WORKFLD $20.
I then have this code (following the example in the documentation for getting the first and last word in the string
REALFLD1 = SCAN(WORKFLD,1,'\');
IF INDEX(WORKFLID,'\') > 0
THEN
REALFLD2 = SCAN(WORKFLD,-1,'\');
It seems to work "backward" - when WORKFLD contains "OPSYS\KNMACTH" the results are REALFLD1 = KNMACTH and REALFLD2 = OPSYS .
If I switch the '1' and the '-1' I get what I want but I'm not sure why it is working this way...
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Ooops.. In my original code (not the 'pseudo code' I posted here) I had REALFLD1 and REALFLD2 reversed as they were close in spelling. Sorry for wasting everyone's brain time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If you want first and last word another column just use catx with scan
catx('-',scan(col1,1,'/'),scan(col1,-1,'/'))
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
No, I want the two values to be in two separate fields, not concatenated.
Please note the slash is '\' and not this '/' (which is one of the default delimiters for the function)
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Works fine for me.
511 data test; 512 have= "OPSYS\KNMACTH" ; 513 first=scan(have,1,'\'); 514 last=scan(have,-1,'\'); 515 put (_all_) (=); 516 run; have=OPSYS\KNMACTH first=OPSYS last=KNMACTH
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
When I run your code, after correcting the WORKFLID to WORKFLD with
237 data junk; 238 WORKFLD="OPSYS\KNMACTH"; 239 REALFLD1 = SCAN(WORKFLD,1,'\'); 240 IF INDEX(WORKFLD,'\') > 0 THEN 241 REALFLD2 = SCAN(WORKFLD,-1,'\'); 242 put _all_; 243 run; WORKFLD=OPSYS\KNMACTH REALFLD1=OPSYS REALFLD2=KNMACTH _ERROR_=0 _N_=1
I do not get your stated result.
Since you likely did not paste code from your actual program then it is likely that you either 1) reversed the names of the REALFLD variables OR used the 1 and -1 in the incorrect code location.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Ooops.. In my original code (not the 'pseudo code' I posted here) I had REALFLD1 and REALFLD2 reversed as they were close in spelling. Sorry for wasting everyone's brain time.