Example of comparing SAS scan function to Python split function

SAS Version:

data _null_;
  name1 = "Richard Thornton";
  fname = scan(name1,1);
  lname=scan(name1,2);
    print fname;
    print lname;
run;


Python 3.x Version:

name1 = "Richard Thornton"
fname=name1.split(" ")[0]
lname=name1.split(" ")[1]
print(fname)
print(lname)

explanation:  The split() function takes the string and turns the individual elements into a new variable which is essentially a list, based on the delimiter passed.

To reference, each item in the list, an offset is passed via [0] or [1].  Python is like C, it references from 0, not 1, as SAS does.

Comments

Popular posts from this blog

Resume of Richard Thornton