SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
TimH
Quartz | Level 8

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...

1 ACCEPTED SOLUTION

Accepted Solutions
TimH
Quartz | Level 8

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.

 

View solution in original post

5 REPLIES 5
SwagatPatil
Fluorite | Level 6

If you want first and last word another column just use catx with scan

 

catx('-',scan(col1,1,'/'),scan(col1,-1,'/'))

TimH
Quartz | Level 8

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)

Tom
Super User Tom
Super User

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
ballardw
Super User

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.

 

TimH
Quartz | Level 8

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.

 

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
  • 5 replies
  • 2134 views
  • 0 likes
  • 4 in conversation