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;
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;
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.