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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 831 views
  • 6 likes
  • 4 in conversation