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!
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.
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.
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;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.