I need to write to a pipe delimited file using the File Writer transformation in DI Studio (4.902)
There are two requirements in the spec that are giving me problems.
1) The header row (Data set variables) needs to have a leading and ending pipe.
2) The observations need to have a leading and ending pipe. I have solved this inelegantly by using the CAT function on the first and last variable in the data set to add the pipe directly to the values in the mapping tab. ex. (CAT('|','<Value>'))
I'd prefer a better solution.
The text in the file needs to look like this:
|Name|Age|City| <---- Variable
|Sam|42|Denver| <---- Observations
|Jill|37|Chicago|
I have no idea how to solve number one. Can't just cheat and add a pipe to the variable name since a pipe is an invalid character.
I have solved it!
Like most problems, the solution was right in front of my face.
Using the File Writer transformation, I generated the code as before. Standard pipe delimited with the values of the first and last variable in the "Mappings" tab concatenated with leading and trailing pipes.
Then, to get the same on the first row of variable names, once the code was created by the transformation, I went to the code tab and changed the drop down to "All user written code" and simply added the pipes at the beginning and the end of the put statement.
if ( _n_ = 1 ) then
do;
put
"|<var1>|<var2>|<var3>|<var4>|<var5>|<var6>|<var7>)|";
end;
I met with the vendor today and the file spec requirement is correct. I agree that it is unusual. We are giving them a carriage return on each line and that should be sufficient, but I guess not.
I have solved it!
Like most problems, the solution was right in front of my face.
Using the File Writer transformation, I generated the code as before. Standard pipe delimited with the values of the first and last variable in the "Mappings" tab concatenated with leading and trailing pipes.
Then, to get the same on the first row of variable names, once the code was created by the transformation, I went to the code tab and changed the drop down to "All user written code" and simply added the pipes at the beginning and the end of the put statement.
if ( _n_ = 1 ) then
do;
put
"|<var1>|<var2>|<var3>|<var4>|<var5>|<var6>|<var7>)|";
end;
Thanks for the ideas.
One thing I did do was attach a document to the transformation detailing what I did and why so the next person through has context and understanding of the code changes.
I am a huge proponent of in-code detailed documentation.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.