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...
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.
If you want first and last word another column just use catx with scan
catx('-',scan(col1,1,'/'),scan(col1,-1,'/'))
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)
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
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.
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.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.