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

I am misunderstanding using multiple set statements.  Here is a piece of code, it's an idea I have but not getting me what I want:

data D3A;

set complete_sort;

by power_id;

count+1;

if first.power_id then count = 1;

where pass_date <> . and '30aug2012'd <= default_date_end <= '01jul2013'd;

if count > 1;

output;

set complete_sort;

run;

I would like to take count > 1 from one dataset and then join it back to the original dataset and pull all of the records, including where count = 1.  Can you show me how to do that in one step with multiple set statements?

Thank You,

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

For this approach, you do need two SET statements.  But the structure will change considerably.

data want;

   count=0;

   do until (last.power_id);

      set complete_sort;

      by power_id;
      if pass_date <> . and '30aug2012'd <= default_date_end <= '01jul2013'd then count + 1;

   end;

   do until (last.power_id);

      set complete_sort;

      by power_id;

      if count > 0 then output;

   end;

run;

The top DO loop processes one POWER_ID, coming up with a COUNT value.  Then the bottom DO loop reads the same observations a second time, and outputs (depending on the value of COUNT).

Here are a couple of basic principles that may help.

Each SET statement operates 100% independently of any other SET statement.  Each reads the first observation, then the second, then the third, etc.

When the DATA step contains an OUTPUT statement, there is no automatic output for every observation.  Instead, OUTPUT only takes place where the OUTPUT statement appears.

Good luck.

View solution in original post

7 REPLIES 7
mohamed_zaki
Barite | Level 11

Not sure  what do you want to do.

Why not provide sample datasets.

You can check Multiple Set Statements in a Data Step: for good examples.

Steelers_In_DC
Barite | Level 11

have

id count

1 1

1 2

1 3

2 1

2 2

3 1

4 1

5 1

want

id count

1 1

1 2

1 3

2 1

2 2

stat_sas
Ammonite | Level 13

May be sql can do it more easily.

proc sql;

create table want as

select * from have

group by id

having count(*)>1

order by id, count;

quit;

Steelers_In_DC
Barite | Level 11

I know how to do it in sql, I'm trying to get a better understanding of multiple set statements and haven't any good literature for this example online.

Thanks though,

Astounding
PROC Star

For this approach, you do need two SET statements.  But the structure will change considerably.

data want;

   count=0;

   do until (last.power_id);

      set complete_sort;

      by power_id;
      if pass_date <> . and '30aug2012'd <= default_date_end <= '01jul2013'd then count + 1;

   end;

   do until (last.power_id);

      set complete_sort;

      by power_id;

      if count > 0 then output;

   end;

run;

The top DO loop processes one POWER_ID, coming up with a COUNT value.  Then the bottom DO loop reads the same observations a second time, and outputs (depending on the value of COUNT).

Here are a couple of basic principles that may help.

Each SET statement operates 100% independently of any other SET statement.  Each reads the first observation, then the second, then the third, etc.

When the DATA step contains an OUTPUT statement, there is no automatic output for every observation.  Instead, OUTPUT only takes place where the OUTPUT statement appears.

Good luck.

Steelers_In_DC
Barite | Level 11

Awesome, thanks.

Steelers_In_DC
Barite | Level 11

I changed the code around a little for the exact result I want, this works great, I appreciate the extra explanation you add.  That was a big help:

data want;

   do until (last.power_id);

      set complete_sort;

      by power_id;

      count+1;

      if first.power_id then count = 1;

   end;

   do until (last.power_id);

      set complete_sort;

      by power_id;

      if count > 1 then output;

   end;

/*         where pass_date <> . and '30aug2012'd <= default_date_end <= '01jul2013'd;*/

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 1042 views
  • 4 likes
  • 4 in conversation