BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Shawnty
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

Shawnty
Obsidian | Level 7
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 😉

I appreciate both your and Astoundings input. I will look into it and see i
Astounding
PROC Star

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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