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

Is there anyway to use an input @ statement and have it scan text right to left instead of left to right?  I have the following code 

data mydata;
infile _filename_ truncover scanover;
input @'string' variable $X.
run;

and am trying to extract X characters before "string," not after, and for the life of me, I cannot figure it out.  My program as a whole works if I wanted to extract X characters after the string "text" was found.

 

Thanks in advance!

 

1 ACCEPTED SOLUTION
5 REPLIES 5
Tom
Super User Tom
Super User

You cannot use TRUNCOVER and SCANOVER at the same time.  I suspect that the last one mentioned is what SAS will use.

Did you try cursor movement commands?

data mydata;
  infile cards ;
  input @'string' +(-16) variable $10. ;
cards;
This line has string in middle
This line has it at the end string 
Short string
does not appear at all
;

You might need to play around to deal with boundary conditions.

data mydata;
  infile cards column=cc ;
  input @'string' @ ;
  found=cc;
  offset = min(cc - length('string') - 10 , cc);
  input @offset variable $10. ;
cards;
This line has string in middle
This line has it at the end string 
Short string
does not appear at all
;

 

Ksharp
Super User

If you have multiple 'string' in a row, could use PRXNEXT().

 

data mydata;
infile cards truncover;
input;
p=prxmatch('/.{4}(?=string)/i',_infile_);
if p=0 then p=length(_infile_)+1;
want=substrn(_infile_,p,4);
cards;
This line has string in middle
This line has it at the end string 
Short string
does not appear at all
;
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @phdibart 

 

I have always found input with @'string' too inflexible compared to reading the whole line into SAS. Using the automatic variable _infile_ is easier, because you can use the full arsenal of SAS string functions.

 

This example takes the last 10 characters before 'string' regardless of blanks etc, but it would be just as easy to search for 'string' as a word and extract the last word before that, or whatever would be needed in your case.

 

File content:

This line has string in middle
This line has it at the end string
Short string
does not appear at all
thisisalongline1234567890stringandmore

 

%let string = string;
data want (drop=pos);
	infile 'c:\temp\testfile.txt';
	input;
	pos = find(_infile_,"&string",'i');
	if pos > 10 then var = substr(_infile_,pos-10,10);
run;

 

 

 

phdibart
Fluorite | Level 6

:edit:  I answered my question.

 

Thank you all for your suggestions.  The following code worked.

%let lltract=32.3861,-99.7583;
%let llmsa=32.4101,-99.8114;

filename google url "http://maps.google.com/maps?daddr=&llmsa.%nrstr(&saddr)=&lltract";

data drive (drop = m);
infile google scanover;
llmsa = symget('llmsa');
lltract = symget('lltract');
m = -13;
input @'miles\"'+m miles $34.;
run;

filename google clear;

You can use truncover with scanover.  See example #3.  http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000146932.htm

 

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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
  • 1302 views
  • 0 likes
  • 5 in conversation