Urgent: Using several set- options in a datastep

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Urgent: Using several set- options in a datastep

Hello,

i have a rather urgent problem which occurred to me today as i was writing a quite complex program today. I've used a dummy dataset to create the logic i needed for the program to work. But have realized it does not work on the real data due to  missing information (Gonna focus more on realistic dummydata next time).


The program is written in a macro where there are a lot of set statements depends on a several if-statements and such. Therefore i can not write it as the "collect_working" is structured.  Below is an illustration of the problem:

 

data dummydata;
input flag1 flag2 flag3 val;
datalines;
1 1 1 33
1 0 . 44
;
run;

data collect_notworking;
set work.dummydata(where=(flag1 =1 and flag2=0 and flag3=1));
output;
set work.dummydata(where=(flag1 =1 and flag2=1 ));
output;

run;

 

data collect_working;
set work.dummydata(where=(flag1 =1 and flag2=0 and flag3=1))
work.dummydata(where=(flag1 =1 and flag2=0 ));
run;

 

 

The problem that arises in collect_notworking is the first set-statement contains 0 observations. After this the datastep seems to stop and does not handle the next set-statement.

Collect_working handles everyting in one set-statement and handles this problem.

So my question is: Is there a way to handle 0-observation cases as in this example with multiple set-statements?

 

I've tried to look into the exist-function and such but have not been able to successfully use it.

Thank you on advance!


Accepted Solutions
Solution
‎11-21-2017 11:37 AM
Super User
Super User
Posts: 8,609

Re: Urgent: Using several set- options in a datastep

What is it your actually trying to do?  If the set statement has nothing to read then the datastep ends.  This code:

data collect_notworking;
  set work.dummydata(where=(flag1 =1 and flag2=0 and flag3=1));
  output;
  set work.dummydata(where=(flag1 =1 and flag2=1 ));
  output;
run;

Doesn't make any sense.  If you want a dataset with all the records from the two wheres then this is the code:

data collect_notworking;
  set work.dummydata (where=(flag1 =1 and flag2=0 and flag3=1))
        work.dummydata (where=(flag1=1 and flag2=1));
run;

Or in fact you could just do:

data collect_notworking;
  set work.dummydata (where=((flag1 =1 and flag2=0 and flag3=1) or (flag1=1 and flag2=1)));
run;

This "The program is written in a macro where there are a lot of set statements depends on a several if-statements and such. Therefore i can not write it as the "collect_working" is structured.  Below is an illustration of the problem:" - just sounds like your trying to fight the SAS system, and read/write each line yourself - which isn't going to work. 

View solution in original post


All Replies
Solution
‎11-21-2017 11:37 AM
Super User
Super User
Posts: 8,609

Re: Urgent: Using several set- options in a datastep

What is it your actually trying to do?  If the set statement has nothing to read then the datastep ends.  This code:

data collect_notworking;
  set work.dummydata(where=(flag1 =1 and flag2=0 and flag3=1));
  output;
  set work.dummydata(where=(flag1 =1 and flag2=1 ));
  output;
run;

Doesn't make any sense.  If you want a dataset with all the records from the two wheres then this is the code:

data collect_notworking;
  set work.dummydata (where=(flag1 =1 and flag2=0 and flag3=1))
        work.dummydata (where=(flag1=1 and flag2=1));
run;

Or in fact you could just do:

data collect_notworking;
  set work.dummydata (where=((flag1 =1 and flag2=0 and flag3=1) or (flag1=1 and flag2=1)));
run;

This "The program is written in a macro where there are a lot of set statements depends on a several if-statements and such. Therefore i can not write it as the "collect_working" is structured.  Below is an illustration of the problem:" - just sounds like your trying to fight the SAS system, and read/write each line yourself - which isn't going to work. 

Contributor
Posts: 24

Re: Urgent: Using several set- options in a datastep

Hi RW9,

Absolutly im trying to fight SAS. Im still a rookie and my know-how is very limited. Therefore i have to go trial by fire and learn by experience. And as i struggle with my problems, eventually i will learn the SAS-way Smiley Wink

I appreciate both your and Astoundings input. I will look into it and see i
Super User
Posts: 5,963

Re: Urgent: Using several set- options in a datastep

Assuming you really must do it that way, here is a workaround.  Other similar workarounds would be possible, if you have some requirements about the order of the observations in the final data set.

 

data want;

if end1=0 then do;
   set work.dummydata(where=(flag1 =1 and flag2=0 and flag3=1)) end=end1;
   output;

end;

if end2=0 then do;
   set work.dummydata(where=(flag1 =1 and flag2=1 )) end=end2;
   output;

end;

if end1 and end2 then stop;

run;

 

But as both you and @RW9 suggested, there are plenty of ways to do this with one SET statement.  Here's one more:

 

data want;

set work.dummydata (where=(flag1=1));

if flag2=1 or (flag2=0 and flag3=1);

run;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 98 views
  • 0 likes
  • 3 in conversation