BookmarkSubscribeRSS Feed
GN0001
Barite | Level 11

Hello team,

I wonder what second set statement do? 

data mydata (Keep=PlanID plandate);
set enrollment1 enrollment2; /*This set statement, appends two data sets: enrollment1 and enrollment2 */
set abc.grpTemp;/* what does this second set do?*/
Run;

Regards,

blueblue

Blue Blue
3 REPLIES 3
ballardw
Super User

Suggestion: Make three small data sets. The first two should be structured similar (same number of variables with same names and types) and the third one contain completely different data. Make the third data set with more records than the first two combined.

See what happens when you use them in similar code. Hint: do not use the KEEP option. See what happens without it first.

Quentin
Super User

The SET statement reads a record from a dataset.

each time

set enrollment1 enrollment2;

executes, it will read a record from enrollment1 or enrollment2 (after it has finished reading records from enrollment1)

 

Each time

set abc.grpTemp;

executes it will read a record from abc.grpTemp.

 

This is an unusual coding pattern.  Your final result will depend in part on the number of records in each dataset.

 

Understanding this step will give you a good understanding of the DATA step.  I would play around with a small example of this data, and add PUT statements, or play around in the DATA step debugger.

 

data mydata (Keep=PlanID plandate);
set enrollment1 enrollment2; /*This set statement, appends two data sets: enrollment1 and enrollment2 */
put _ALL_;
set abc.grpTemp;/* what does this second set do?*/
put _ALL_;
Run;

That said, I wouldn't trust a step like this.  If abc.grpTemp does not have the expected number of records, you could end up with some surprising results.  And also there is a possibility of variable collisions.

 

The Boston Area SAS Users Group is hosting free webinars!
Next up: Lisa Mendez & Richann Watson present Get Tipsy with Debugging Tips for SAS® Code: The After Party on Wednesday Jul 16.
Register now at https://www.basug.org/events.
mkeintz
PROC Star

The first SET statement, as you realize, reads data from enrollment1 (N=n1)  and appends data from enrollment2 (N=n2).

 

For each of those n1+n2 cases, the second SET reads an observation form abc.grptemp (N=n3).

 

EXCEPT: the data step will stop when either of the two SETs reach end of data.  MYDATA will have MIN(n3,n1+n2) observations.

 

The are other issues as well.  For example, any variable that is both in abc.grptemp and the appended sequence enrollment1+enrollment2 will end up with the value taken from the second SET.   So if you reversed the order of the SET statements, you'd end up with the same number of observations, but different values in such overlapping variables.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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