BookmarkSubscribeRSS Feed
rookie72
Calcite | Level 5
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;
6 REPLIES 6
deleted_user
Not applicable
This is screwy.
Don't do it the hard way.
Use two data steps.
Doc_Duke
Rhodochrosite | Level 12
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.
rookie72
Calcite | Level 5
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.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Your DO / END loop will only execute once, given what you have specified for UNTIL().


Scott Barry
SBBWorks, Inc.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
deleted_user
Not applicable
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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 999 views
  • 0 likes
  • 4 in conversation