BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Louis_DB
Fluorite | Level 6

Hey, 

I'm trying to see if there is a specific value (in this case the letter "Y") after a set number of specific symbols (in this case in between the last 11 and 12 "|"s). 

I tried to save the value in a new column and then doing an if statement, but the issue is that I do not know how to print the letter just before the last 11 |'s.

 

e.g.

data have;
input params $70.;
cards;
|bla|||||5||bla|bla||||||bla||||bla|||||||Y|||||||||||
|bla|||||10||bla|bla||||||bla||||bla|||||||Y|baa||||||||||
|bla|||||10||bla|bla||||||bla||||bla||||||||baa||||||||||
;

data want;
set have;
Want = substr(params, length(params)-11,1);
run;

In this example i wanted it to print:

1: Y

2: Y

3: nothing

and not

1: Y

2: a

3: a

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input params $70.;
cards;
|bla|||||5||bla|bla||||||bla||||bla|||||||Y|||||||||||
|bla|||||10||bla|bla||||||bla||||bla|||||||Y|baa||||||||||
|bla|||||10||bla|bla||||||bla||||bla||||||||baa||||||||||
;

data want;
set have;
call scan(params,-12,p,l,'|','mq');
want=substrn(params,p,l);
drop p l;
run;

View solution in original post

5 REPLIES 5
Ksharp
Super User
data have;
input params $70.;
cards;
|bla|||||5||bla|bla||||||bla||||bla|||||||Y|||||||||||
|bla|||||10||bla|bla||||||bla||||bla|||||||Y|baa||||||||||
|bla|||||10||bla|bla||||||bla||||bla||||||||baa||||||||||
;

data want;
set have;
call scan(params,-12,p,l,'|','mq');
want=substrn(params,p,l);
drop p l;
run;
yabwon
Onyx | Level 15

Wouldn't just "backward" scan() be good?

data want;
  set have;
  want = scan(params,-12,"|","M");
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



Ksharp
Super User
Sure. In this case, both are same . But yours could save one line code .
Tom
Super User Tom
Super User

SCAN() is better in this case since the request was just for the value between the delimiters, not the rest of the string.

Ksharp
Super User
Tom,
SCAN() and CALL SCAN() are the same function and rountine .
This could save one line code, if it was what you are thinking , Whatever .

sas-innovate-wordmark-2025-midnight.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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1104 views
  • 6 likes
  • 4 in conversation