I'm using this datastep to interleave two datasets. I need to include an option to output dataset opt4interleave to dataset interleave in the event that the dataset fixed4interleave is empty.
Thanks in advance for any advice you can offer,
Gene
data interleave;
format target $20.;
do _N_=1 by 1 until(last.Target);
set fixed4interleave (in=ina) opt4interleave (in=inb);
by Target;
N_A+ina;
N_B+inb;
end;
do _N_=1 to _N_;
set fixed4interleave opt4interleave curobs=curobs1;
by Target;
if N_A and N_B then output;
end;
call missing(N_A,N_B);
run;
So check that also.
data want;
if 0 then set one nobs=nobs;
merge one(keep=id in=in1) two(keep=id in=in2);
by id;
if first.id;
do until (last.id);
set one two;
by id;
if (in1 or 0=nobs) and in2 then output;
end;
run;
If the first dataset is a view that prevents NOBS from working then just add an extra step to set a macro variable instead.
data _null_;
call symputx('nobs',0);
set one;
call symputx('nobs',1);
stop;
run;
data want;
merge one(keep=id in=in1) two(keep=id in=in2);
by id;
if first.id;
do until (last.id);
set one two;
by id;
if (in1 or 0=&nobs) and in2 then output;
end;
run;
Check NOBS in DICTIONARY.TABLES for your dataset, and then use %IF-%THEN-%DO to run code conditionally:
proc sql noprint;
select nobs into :mynobs
from dictionary.tables
where libname = "WORK" and memname = "FIXED4INTERLEAVE";
quit;
%if &mynobs.
%then %do;
/* your code */
%end;
%else %do;
data interleave;
set opt4interleave;
run;
%end;
Your description is too short to understand exactly what you mean. And I don't understand what your posted data step is trying to do.
To interleave two datasets just use code like:
data want;
set one two;
by id;
run;
What is it that you want to do differently than that?
If you want to exclude BY groups from TWO that do not appear in ONE then add a retained variable to remember if there were any observations for that group in ONE.
data want;
set one(in=in1) two(in=in2);
by id;
if first.id then any=0;
retain any;
if in1 then any=1;
if in2 and not any then delete;
drop any ;
run;
Perhaps you are trying to only write observations for BY groups that appear in both?
data want;
merge one(keep=id in=in1) two(keep=id in=in2);
by id;
if first.id;
do until (last.id);
set one two;
by id;
if in1 and in2 then output;
end;
run;
Thanks for taking the time to respond,
You are correct that I am trying to only write observations for BY groups that appear in both datasets. But if dataset one is empty, I would like the code to simply pass dataset two through to dataset want.
Thanks,
Gene
So check that also.
data want;
if 0 then set one nobs=nobs;
merge one(keep=id in=in1) two(keep=id in=in2);
by id;
if first.id;
do until (last.id);
set one two;
by id;
if (in1 or 0=nobs) and in2 then output;
end;
run;
If the first dataset is a view that prevents NOBS from working then just add an extra step to set a macro variable instead.
data _null_;
call symputx('nobs',0);
set one;
call symputx('nobs',1);
stop;
run;
data want;
merge one(keep=id in=in1) two(keep=id in=in2);
by id;
if first.id;
do until (last.id);
set one two;
by id;
if (in1 or 0=&nobs) and in2 then output;
end;
run;
Hi Tom,
I adopted your suggested code to my problem but dataset want was not populated with dataset two. See below for log output. Thanks for trying to help.
Gene
You have a typo (or perhaps I did?).
Notice that SAS tells you about it:
NOTE: Variable nobs is uninitialized.
Fix this line:
if 0 then set fixed4interleave nobs=nob;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.