I have a two sas data sets with same variables but different observations. I want to convert these data sets into two separate raw files in one data step. I have written the following code.
I can generate the first raw file but for some reason I can not generate the second raw file ( file2.txt) with this code.
Can some one please identify what am i doing wrong here? Is there any other alternative?
Thanks for your all help.
i = 1;
do until (i le 3);
if i = 1 then do;
file "C:\file.txt" ;
file "C:\file2.txt" ;
@1 variable1 z16. -r
@17 variable2 16. -l
@33 variable3 $ 16.
If I use RETAIN i 1; statement then I can generate the first raw file(file1.txt) but with only one observation. For some reason it is not writing all observations into the raw file. However it generates the second raw file correctly.
use an interative loop to avoid having to set i, etc.
do i=1 to 2;
but this will output only one observation from each dataset.
There are many ways to accomplish what you want in a single data step, but it is still better to simply use two data steps since doing it in one doesn't save anything and only increases the complexity of the process.
One way is to use a single "set" statement = set dataset1(in=in1) dataset2(in=in2);
then use an "if" conditional = if in1 then do; ...; end;
Another way is to use the NOBS=N option
do i=1 to A+B;
if mod(i,2) then do;
set dataset1 nobs=A;
set dataset2 nobs=B;
this is flawed in that if dataset1 has 5 observations and dataset2 has 12, you will get weird results or an error.
These I just thought of, and there are probably still others.
I suspect you have the wrong perspective of what SAS is.
SAS is not a process nor a machine control perspective language.
SAS is data centric.
It assumes the data is in a table, in a file and that you want to process one observation at a time. It simplifies the opening and closing of those files, their reading, writing and iteration through them.
Do not think in terms of C, Java, Pascal, COBOL, Fortran, PL/1, etc. languages which are about process or machine control.
Think instead of I have data, I want to do this with the data, waza.
The simplest thing to do in this case is
put ... ;
put ... ;
It's simple, easy to understand and easy to write, the whole point behind SAS.
doing this in one data step only confuses the issue, wastes time, provides opportunities for all kinds of errors, and is probably less efficient because of all the extra processing you have to have SAS do to keep straight what is going on.