DATA Step, Macro, Functions and more

interleaving 2 datasets with SET statement

Reply
Frequent Contributor
Posts: 89

interleaving 2 datasets with SET statement

why does my final output dataset has only 2 rows and not 6 (for each date a prod line)?

data dates;
input date $ ;
cards;
01/02/2010
02/02/2010
03/02/2010
;
run;
data prods;
input prod $ ;
cards;
PRODA
PRODB
;
run;

data prods_dates;
set dates;
do order=1 to LAST;
set prods nobs=LAST;
output;
end;
RUN;
N/A
Posts: 0

Re: interleaving 2 datasets with SET statement

hello,

your final output has only 2 rows because SAS reads sequentially which determines the end of prods file to be "achieved" on the first pass to the data step.

you expected on the second pass to the data step that SAS reads the prods file from the beginning, but instead SAS stops execution and this is because SAS reached the end of prods file.

one solution is using direct access to read the prods file:

[pre]
data prods_dates;
set dates;
i=1;
do order=1 to LAST;
set prods point=i nobs=LAST ;
output;
i+1;
end;
RUN;
[/pre]

Marius
Super Contributor
Super Contributor
Posts: 3,174

Re: interleaving 2 datasets with SET statement

The SAS variable LAST is incremented until it reaches the NOBS= value (based on WORK.PRODS). Then, unless you were to reset the value to LAST=0; outside your DO /END loop, the "expression" condition will never be met again.

For self-diagnosis, suggest you add the following statement at various points in your SAS program to display all SAS variable values:

PUTLOG '>DIAG>' / _ALL_;

Scott Barry
SBBWorks, Inc.
Frequent Contributor
Posts: 89

Re: interleaving 2 datasets with SET statement

Marius, thx for solution and reason why it did not work ;-)

CU,
Herman
Ask a Question
Discussion stats
  • 3 replies
  • 151 views
  • 0 likes
  • 3 in conversation