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

Hi,

I have a data file. "data.ods". But this file contain some pattern that I need to replace. (Replace the pattern "|^'|" to "|^-|"). So I implemented a data step to do that and It worked as expected. After this conversion I need to save the results as a new file (data1.txt).   However the data in data1.txt seems got truncated after some number of characters in each row.

(data in data.ods has the delimiter |^)

 

As an example: let say that data.ods looks like this  (using toy data)

abs|^ssass|^uuwawaaa|^hdadaaaaaaaaaaaaaaaaaaaaaaaaaaa|^sasasssas
122|^sasa|^'|^sassasasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaassasasas|^dasas
432|^ssadasa|^sassa|^sassasasaaaaaaaaaaaaaaaaaaaaaaaaaaaahsasaassdada|^ffwa
3122|^sasdada|^'|^sassasasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadadada|^fdaaas

 

Then data1.txt looks like this:

 

abs|^ssass|^uuwawaaa|^hdadaaaaaaaaaaaaaaaaaaaaaaaaaaa|^sa
122|^sasa|^-|^sassasasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
432|^ssadasa|^sassa|^sassasasaaaaaaaaaaaaaaaaaaaaaaaaaaaa
3122|^sasdada|^-|^sassasasaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

 

Here is my code: Could anyone help me to resolve this issue?  Thank you very much

 

filename ipt_file "data.ods";
%let out_file=data1.txt;
data _null_;
  infile ipt_file truncover;
  file "&out_file" ; 
  input;
  modified_line = tranwrd(_infile_, "|^'|", "|^-|");

  /* Output the modified_line with a new line character */
  put modified_line;
  datalines;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You don't want the DATALINES statement in that last data step since you want to read from the INFILE statement instead.

 

You did not define a length for the variable you created, so SAS will default it to $200 since you are assigning it the result of a character function.

 

Also since you did not use a FORMAT when writing the line any leading spaces will be removed.  Which is probably fine since it looks like your lines are delimited, so leading spaces would normally be ignored anyway.

 

Since you are not changing the length of the string (you are replacing 4 characters with 4 other characters) just use the _INFILE_ variable as the target.  Note that using _INFILE_ will limit the line length to 32K bytes.

data _null_;
  infile ipt_file ;
  file "&out_file" ; 
  input;
  _infile_= tranwrd(_infile_, "|^'|", "|^-|");
  put _infile_;
run;

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You don't want the DATALINES statement in that last data step since you want to read from the INFILE statement instead.

 

You did not define a length for the variable you created, so SAS will default it to $200 since you are assigning it the result of a character function.

 

Also since you did not use a FORMAT when writing the line any leading spaces will be removed.  Which is probably fine since it looks like your lines are delimited, so leading spaces would normally be ignored anyway.

 

Since you are not changing the length of the string (you are replacing 4 characters with 4 other characters) just use the _INFILE_ variable as the target.  Note that using _INFILE_ will limit the line length to 32K bytes.

data _null_;
  infile ipt_file ;
  file "&out_file" ; 
  input;
  _infile_= tranwrd(_infile_, "|^'|", "|^-|");
  put _infile_;
run;

 

sam88r
Fluorite | Level 6
Thank you very much. This worked for me. Appreciate your help

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!

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
  • 2 replies
  • 433 views
  • 2 likes
  • 2 in conversation