Reputation: 115
I am trying to write a file using some variables stored in an external file. I have a trouble using line holder(@@ with put statement) and loop. I like to put variables in some places in the file but the resut file I got repeats by each line of those variables. I just want those variables to be placed at one time as a chunk.
Here is the code I wrote:
data _null_;
set merged;
file 'c:outfile.txt';
put @1 "A" @4 &totaln.;
do i=1 to &totaln;
put @1 i @4 "L3 2 DW 1.7" @17 a @28 b @40 c @@;
end;
put;
put @1 "OL" @4 &totaln.;
do j=1 to &totaln.;
put @1 j put @4 "L3 2 DW 1.7" put @17 av @28 bv @40 cv @@;
end;
put;
put @1 "CI &totaln. AO";
put @1 "OP";
put @1 "KO" @4 "SL";
run;
&totaln. is the total number of count in the merged file that i made. 'Merged' contains a, b, c, av, ab, ac variables ( their rows are about 19 of them). So i want 19 rows of a,b,c variables below NE totaln line. And then the same thing for av, bv, cv below OL totaln line then the three lines of CI OP and Ko lines at the end of the outfile.
merged:
A B C av bv ac cnt
0.0268 676.9155 0.0347 0.9620294118 -2.131070959 0.031533299 1
0.0215 704.6740 0.2240 0.7738647059 -1.185638164 0.3506849035 2
0.0430 736.3752 0.2053 1.5611941176 -0.442240668 0.2434476134 3
0.0223 727.6945 0.1752 0.8075882353 -0.833957317 0.1725987826 4
0.0220 715.3714 0.1187 0.7434058824 -0.991003252 0.120438949 5
0.0287 754.1052 0.1824 0.8567470588 0.126161198 0.1685302022 6
outfille look:
NE 6
1 L3 2 DW 1.7 0.0268 676.9155 0.0347
2 L3 2 DW 1.7 0.0215 704.6740 0.2240
3 L3 2 DW 1.7 0.0430 736.3752 0.2053
4 L3 2 DW 1.7 0.0223 727.6945 0.1752
5 L3 2 DW 1.7 0.0220 715.3714 0.1187
6 L3 2 DW 1.7 0.0287 754.1052 0.1824
OL 6
1 L3 2 DW 1.7 0.9620294118 -2.131070959 0.031533299
2 L3 2 DW 1.7 0.7738647059 -1.185638164 0.3506849035
3 L3 2 DW 1.7 1.5611941176 -0.442240668 0.2434476134
4 L3 2 DW 1.7 0.8075882353 -0.833957317 0.1725987826
5 L3 2 DW 1.7 0.7434058824 -0.991003252 0.120438949
6 L3 2 DW 1.7 0.8567470588 0.126161198 0.1685302022
CI 6 AO
OP
KO SL
Upvotes: 0
Views: 438
Reputation: 28411
In order to split the data set down the middle (essentially) and put the left 3 variables above the right 3 variables in the output file...you need to 2 data steps. I split the variables into 2 data sets. Once the output file is created with the 1st 3 variables, it can be written to again using the MOD option on the FILE statement. You probably need to adjust some of your Put @ locations to make the variables align into columns. Also, in this example it appears that "L3 2 DW 1.7" is a constant, put that in a macro variable so that it only needs to be changed in one location.
Try this:
proc sql;
select count(*) into:totaln
from merged;
quit;
data merged1(keep=cnt a b c) merged2(keep=cnt av bv ac);
input A B C av bv ac cnt;
datalines;
0.0268 676.9155 0.0347 0.9620294118 -2.131070959 0.031533299 1
0.0215 704.6740 0.2240 0.7738647059 -1.185638164 0.3506849035 2
0.0430 736.3752 0.2053 1.5611941176 -0.442240668 0.2434476134 3
0.0223 727.6945 0.1752 0.8075882353 -0.833957317 0.1725987826 4
0.0220 715.3714 0.1187 0.7434058824 -0.991003252 0.120438949 5
0.0287 754.1052 0.1824 0.8567470588 0.126161198 0.1685302022 6
;
run;
data _null_;
set merged1;
file "C:\outfile.txt";
if _n_=1 then put @1 "NE" @4 "&totaln";
put @1 cnt @4 "L3 2 DW 1.7" @17 a @28 b @40 c;
run;
data _null_;
set merged2 end=last;
file "C:\outfile.txt" mod;
if _n_=1 then put @1 "OL" @4 "&totaln";
put @1 cnt put @4 "L3 2 DW 1.7" put @17 av @28 bv @40 ac;
if last then do;
put @1 "CI &totaln. AO";
put @1 "OP";
put @1 "KO" @4 "SL";
end;
run;
Upvotes: 1