DATA Step, Macro, Functions and more

How to convert two sas data set into raw file within one data step?

Reply
Occasional Contributor
Posts: 15

How to convert two sas data set into raw file within one data step?

Hi,

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.


data _null_;
i = 1;
do until (i le 3);
if i = 1 then do;
set dataset1;
file "C:\file.txt" ;
end;
else do;
set dataset2;
file "C:\file2.txt" ;
end;
put
@1 variable1 z16. -r
@17 variable2 16. -l
@33 variable3 $ 16.
i+1;
end;
Run;
N/A
Posts: 0

Re: How to convert two sas data set into raw file within one data step?

This is screwy.
Don't do it the hard way.
Use two data steps.
Trusted Advisor
Posts: 2,116

Re: How to convert two sas data set into raw file within one data step?

Your use of the i=1; statement means that i is always valued at 1 when the first IF statement is encountered. Replace i=1 with a RETAIN statement that initializes i to 1.
Occasional Contributor
Posts: 15

Re: How to convert two sas data set into raw file within one data step?

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.
Super Contributor
Super Contributor
Posts: 3,174

Re: How to convert two sas data set into raw file within one data step?

Your DO / END loop will only execute once, given what you have specified for UNTIL().


Scott Barry
SBBWorks, Inc.
Super Contributor
Super Contributor
Posts: 3,174

Re: How to convert two sas data set into raw file within one data step?

Suggestion: investigate using two DO / END loops and code the END= on each of the SET statements, but use different variables, like EOF1 and EOF2.

Then, if you want to have one code piece to perform the PUT, use the LINK statement and create a sub-routine in the DATA step for each of the two DO/END processing segments to reference.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: How to convert two sas data set into raw file within one data step?

use an interative loop to avoid having to set i, etc.

do i=1 to 2;
statements;
end;

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

data _null_;
do i=1 to A+B;
if mod(i,2) then do;
set dataset1 nobs=A;
file "C:\file1.txt";
end;
else do;
set dataset2 nobs=B;
file "C:\file2.txt";
end;
...
end;
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

data _null_;
set dataset1;
file "C:\file1.txt";
put ... ;
run;

data _null_;
set dataset2;
file "C:\file2.txt";
put ... ;
run;

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.
Ask a Question
Discussion stats
  • 6 replies
  • 197 views
  • 0 likes
  • 4 in conversation