Hello,
I was hoping for any ideas on the combination of two or more data sets when exporting to txt that have different fields and lengths. A variety of requests that we receive include uploading txt files where the overall row lengths change from line to line and where the delimiter shifts positions based on field length. Traditionally we have just output the different txt files and manually combined them (copy+paste the second txt file into the body of the first under the last line of the first txt file).
Example of the fake data txt file output lines with the data steps for export:
data export_1;
set part_a_txt;
file "\\filepath.txt" dlm=',';
put id $13. survey $9. section $10. code $16. line $8. race $7. sex $6. count $11.;
run;
data export_2;
set part_b_txt;
file "\\filepath.txt" dlm=',';
put id $13. survey $9. section $10. level $8. line $8. sex $6. count $11.;
run;
Resulting txt file 1: ID=######,SURVEY=1,SECTION=1,CODE=13.0000,LINE=11,RACE=2,SEX=1,COUNT=2
Resulting txt file 2: ID=######,SURVEY=1,SECTION=2,LEVEL=1,LINE=01,SEX=1,COUNT=2
Does anyone know of a way that I could essentially combine the two data steps here that I am using for a single data export step despite the fields and the overall line length being different? I understand I that I could probably just concatenate all of this together into one field, union, and export that way, but I wouldn't call that the most ideal way of handling it and I'm curious if others have any ideas.
I appreciate any advice you have to offer. Thanks.
Evan
data export_1;
set part_a_txt (in=a) part_b_txt (in=b);
file "\\filepath.txt" dlm=',';
if a then
put id $13. survey $9. section $10. code $16. line $8. race $7. sex $6. count $11.;
else if b then
put id $13. survey $9. section $10. level $8. line $8. sex $6. count $11.;
run;
data export_1;
set part_a_txt (in=a) part_b_txt (in=b);
file "\\filepath.txt" dlm=',';
if a then
put id $13. survey $9. section $10. code $16. line $8. race $7. sex $6. count $11.;
else if b then
put id $13. survey $9. section $10. level $8. line $8. sex $6. count $11.;
run;
Exactly what I was looking for. Thank you!
Another possible solution:
data export_1; set part_a_txt; file "\\filepath.txt" dlm=','; put id $13. survey $9. section $10. code $16. line $8. race $7. sex $6. count $11.; run; data export_2; set part_b_txt; file "\\filepath.txt" dlm=',' MOD ; put id $13. survey $9. section $10. level $8. line $8. sex $6. count $11.; run;
Operating system dependent but for the file systems that accept it the MOD option on a File statement adds lines at the end of the existing text.
This may be a tad easier than writing a bunch of if statements when you have more than 2 layouts.
Question though, why are your creating a new data set when you write the files? If you don't actually need a data set use DATA _NULL_; That allows you do anything in data step code it just does not create a data set.
Apologies for the late reply here - I never received an email like I traditionally do when I get a reply. I suppose it is because I already selected a solution? Not sure.
I do use data _null_; frequently and it could be utilized here. I just don't use it when I want to easily review the data set in SAS.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.