BookmarkSubscribeRSS Feed
SIgnificatif
Quartz | Level 8

Hi All.

I'm looking into ways to delete left part of the text before a blank line, i'm giving example of a similar code that remove everything before the '|' character, are there ways to do the same before the blank line ?  ( or keep only the last line) I need only to keep the text on the last line ( where the keword another starts, but the keyword is not constant, so another solution is needed) :

 

data have;
length textstr $100;
input textstr $;
datalines;
Text text text 

Another text , text text
;;;;
run;

data want;
  set have;
  call scan(textstr,2,start,length,'|');
  new_textstr = substr(textstr,start);
run;

I have tried also the compress, where it looks for the kw, but this is something difficult to combine:

new_textstr = compress(textstr,,"kw");



Thank you for response.

12 REPLIES 12
Tom
Super User Tom
Super User

Can you provide a better example?  Did you run your sample program?  I don't think it does what you expect.

 

If you want to remove the characters before a "marker" in a character variable just find its location and use SUBSTR().

loc = index(string,'|');
if loc then string=substr(string,loc);
SIgnificatif
Quartz | Level 8
Thank you for the response,
I need the new line marker, so the text before the new line will be removed ( remove left side from that marker ) , I provided the example with | , because I don't have another example, also how to remove multiple new lines ( and text between them) and keep the last row at the end.
Tom
Super User Tom
Super User

I still don't get the requirement. Are you somehow trying to treat an entire text file as if it was the value of a single variable?

Normally when you read a text file each row in the file becomes a separate observation in your dataset.

 

Do you have the data already in a SAS dataset?  So does that mean you have some variable that contains literal linefeed characters ('0A'x) embedded in with the other characters?

 

Or are you trying to read the text file?  If you are trying to read the text file what data structure are you trying to create from it.

 

Or are you just trying to transform one text file into another text file?

SIgnificatif
Quartz | Level 8

Hi.

Thank you for your quick response,

I'll make it easier , it's not a column text, jsut plain text..., read from a database.
I would like to remove all ('0A'x) and text before the last line of strings, sort of :

Text text text Text text text Text text text 

Text text text Text text text Text text text 
Text text text Text text text Text text text 
Text text text Text text text Text text text 

Text text text Text text text Text text text 


Text text text Text text text Text text text 

 

Only text in red and bold ( last line should remain)
Hope this helps 🙂

Tom
Super User Tom
Super User

Still makes no sense, but it sounds like you have character variable with embedded linefeeds.  You can just treat the linefeeds as delimiters.

Let's make an example dataset.

data have;
  mytext=catx('0A'x,'line one','line two','line three');
run;

data want;
  set have;
  newvar=scan(mytext,-1,'0A'x);
run;

proc print;
run;

image.png

SIgnificatif
Quartz | Level 8
Thank you for the message,
I will add that : I have one input variable, I'm looking to keep only the last line of text in it.

1) I don't know the number of lines , it's *PLAIN TEXT*
2) I don't know the number of '0A'x in the variable
3) How to treat lines of text ( plain text) as a delimiter ? ( it's not about rows and columns, but plain text)

Thank you for response.

Sincerely.
Tom
Super User Tom
Super User

Show what you have.  If it is a FILE the show a few lines of the file.

If it is character strings in a dataset then show the proc contents for the dataset and proc print for a few lines.

 

Show what you want as the result of processing your example input. Again if you want a FILE out then show the file you want. If it is a dataset that show the dataset you want.

 

For example if you have what you posted in example in a FILE named 'have.txt' and you want to write just the last line to a file named 'want.txt' then just run this trivial data step.

data _null_;
  infile 'have.txt' end=eof;
  file 'want.txt';
  input;
  if eof then put _infile_;
run;

 If the rules for which lines to write are more complex then explain them.  Do you want to write just the first line after a blank line? after two blank lines in a row?  All lines after two blank lines until the next blank line (basically selecting a paragraph?)

SIgnificatif
Quartz | Level 8
Thank you for response Dear Tom, but i'm not using the infile for this one, I'm directly using the variable, for instance :
sometext=.....blablaba
Tom
Super User Tom
Super User

Show your HAVE dataset. Something like:

data have;
  row+1;
  input line $char80.;
cards;
Text text text Text text text Text text text 

Text text text Text text text Text text text 
Text text text Text text text Text text text 
Text text text Text text text Text text text 

Text text text Text text text Text text text 


Text text text Text text text Text text text
;

The explain your logic.  In the post with those lines of data you asked to only get the last row.

Why the last row?  Is it because it is the last row?

data want;
  set have end=eof;
  by row;
  if eof;
run;

Is it because it follows two blank lines?

data want;
  set have end=eof;
  if _n_>2 and ' '^= line and ' '=lag(line) and ' '=lag2(line);
run;

Or do you need something more complex? If so then provide examples that demonstrate the complexity you need to solve.

SIgnificatif
Quartz | Level 8

Hi Dear Tom.
Sorry for the late reply, it's almost exactly as mentioned in the last answer ( EOF and lag method) , but i found that there are some lines that are longer, and wanted to precise if it's possible to select the last 'paragrpah' and not the last line ? thank you again.

data have;
  row+1;
  input line $char80.;
cards;Text text text Text text text Text text text 

Text text text Text text text Text text text 
Text text text Text text text Text text text 
Text text text Text text text Text text text 

Text text text Text text text Text text text 


Text text text Text text text Text text text
text text text.
;

 

Tom
Super User Tom
Super User

Sounds like you might want to number the paragraphs?

So assume you already read the data into dataset named HAVE and the lines of text appear in the variable LINE.  Here is a way to assign a new number to each paragraph.

data paragraphs;
  set have;
  if line ne ' ' and lag(line)=' ' then paragraph+1;
run;  

The idea is that you will increase the paragraph number when you are on the first non-blank line.  Using the sum statement will cause the paragraph variable to be initialized to zero and retain its value across iterations of the data step.

 

Do you also need to keep track of double blank lines?  Or does know which lines below to which paragraph solve your problem?

SIgnificatif
Quartz | Level 8

Thx ! that's great, yes I need to track the last double blank line, and how to 'combine' the last paragrpah to make it look like 1 text and not 2 separated lines ?

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 12 replies
  • 1714 views
  • 0 likes
  • 2 in conversation