SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hello_there
Lapis Lazuli | Level 10

Hi, I'm trying to separate this one variable into a 2 variables called pre and post, and i don't understand why i'm having difficulty in doing this using the scan function.

 

data have1; 
infile datalines dsd dlm=",";
length shift $200;
	input shift $;
datalines;
stable to stable,
stable to unstable,
unstable to stable,
zero (empty) to +1 (ok),
+1 (ok) to +2 (good)
;
run;


data want; set have1;
pre=scan(shift, 1, " to ");
post=scan(shift, 2, " to ");
run;

 When I use my code for the data step want, i get this, which is what i don't want:

Hello_there_0-1679494745984.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

SCAN uses the letters in the third argument to split words. Your third argument contains the letter 't', and so on line one, the first "word" ends when a 't' is found, which means the first "word" is 's' (from the text string 'stable to stable');

 

How about this code:

 

data want; 
    set have1;
    where=find(shift,' to ');
    pre=substr(shift,1,where-1);
    post=substr(shift,where+4);
    drop where;
run;

 

--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

SCAN uses the letters in the third argument to split words. Your third argument contains the letter 't', and so on line one, the first "word" ends when a 't' is found, which means the first "word" is 's' (from the text string 'stable to stable');

 

How about this code:

 

data want; 
    set have1;
    where=find(shift,' to ');
    pre=substr(shift,1,where-1);
    post=substr(shift,where+4);
    drop where;
run;

 

--
Paige Miller
Hello_there
Lapis Lazuli | Level 10
This worked perfectly. Thanks, PaigeMiller!
Hello_there
Lapis Lazuli | Level 10

Hi @PaigeMiller , do you know i would account for if there is more than 1 " to ", and i just want what's after the last one?

The last value has 2 " to ".

data have1; 
infile datalines dsd dlm=",";
length shift $200;
	input shift $;
datalines;
stable to stable,
stable to unstable,
unstable to stable,
zero (empty) to +1 (ok),
+1 (ok) to +2 (good)
zero (good to good) to +3 (great)
;
run;


data want; 
    set have1;
    loc=find(shift,' to ');
    pre=substr(shift,1,loc-1);
    post=substr(shift,loc+4);
run;
yabwon
Onyx | Level 15
loc=find(shift,' to ', -(lengthn(shift)));
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Hello_there
Lapis Lazuli | Level 10
Thanks, Bart!
Hello_there
Lapis Lazuli | Level 10
Actually, never mind, there's another variable in my data set that will help me so i don't have to parse this text anymore than i have to. Ignore my previous reply.
yabwon
Onyx | Level 15

Scan() separates over a single character. Try this:

data have1; 
infile datalines dsd dlm=",";
length shift $200;
	input shift $;
datalines;
stable to stable,
stable to unstable,
unstable to stable,
zero (empty) to +1 (ok),
+1 (ok) to +2 (good)
;
run;


data want; 
set have1;
shift1=tranwrd(shift, " to ", "#");
pre=scan(shift1, 1, "#");
post=scan(shift1, 2, "#");
run;
proc print;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Hello_there
Lapis Lazuli | Level 10
Brilliant! Thanks, Bart, that works also!

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.

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
  • 8 replies
  • 1723 views
  • 7 likes
  • 3 in conversation