BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
genemroz
Quartz | Level 8

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

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;
Tom
Super User Tom
Super User

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;
Tom
Super User Tom
Super User

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;
genemroz
Quartz | Level 8

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

Tom
Super User Tom
Super User

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;
genemroz
Quartz | Level 8

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

 

data want;
if 0 then set fixed4interleave nobs=nob;
merge fixed4interleave(in=in1) opt4interleave (in=in2);
by target;
 if first.target;
do until (last.target);
set fixed4interleave opt4interleave;
by target;
if (in1 or 0=nobs) and in2 then output;
end;
run;
 
NOTE: Variable nobs is uninitialized.
NOTE: There were 0 observations read from the data set WORK.FIXED4INTERLEAVE.
NOTE: There were 399680 observations read from the data set WORK.OPT4INTERLEAVE.
NOTE: There were 0 observations read from the data set WORK.FIXED4INTERLEAVE.
NOTE: There were 399680 observations read from the data set WORK.OPT4INTERLEAVE.
NOTE: The data set WORK.WANT has 0 observations and 6 variables.
Tom
Super User Tom
Super User

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;

genemroz
Quartz | Level 8
Thanks, Tom, for helping me through this.

Gene

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 202 views
  • 2 likes
  • 3 in conversation