BookmarkSubscribeRSS Feed
☑ This topic is solved. 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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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