DATA Step, Macro, Functions and more

resetting temporary variable in merge statement

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
Accepted Solution

resetting temporary variable in merge statement

What is the purpose of resetting temporary variables many1 and many2 in following code. 

data pairs oddballs;
	many1=0;
	many2=0;
	merge many1 ( in = many1 )
		many2 ( in = many2 );
	by partkey;

	if many1 and many2 then
		output pairs;
	else output oddballs;
run;

Accepted Solutions
Solution
‎08-03-2016 03:38 PM
Super User
Posts: 5,081

Re: resetting temporary variable in merge statement

This type of code is used for many-to-many matches.  For example, let's say:

 

  • MANY1 contains 3 observations per PARTKEY
  • MANY2 contains 5 observations per PARTKEY
  • The objective is to output 3 observations per PARTKEY, matching the first from MANY1 with the first from MANY2, the second from MANY1 with the second from MANY2, etc.  Discard any that are left over from the data set containing more observations.

That's what this program accomplishes ... matching based on whichever data set has fewer observations, and putting those matched observations into PAIRS.  Discarding any that are left over without a match, putting them into ODDBALLS.

View solution in original post


All Replies
Super User
Posts: 10,500

Re: resetting temporary variable in merge statement

No practical purpose served that I can tell. If you found this code somewhere the assignments might be an artifact left in from previous code tested.

Or someone misunderstanding how temporary variables work in SAS.

Solution
‎08-03-2016 03:38 PM
Super User
Posts: 5,081

Re: resetting temporary variable in merge statement

This type of code is used for many-to-many matches.  For example, let's say:

 

  • MANY1 contains 3 observations per PARTKEY
  • MANY2 contains 5 observations per PARTKEY
  • The objective is to output 3 observations per PARTKEY, matching the first from MANY1 with the first from MANY2, the second from MANY1 with the second from MANY2, etc.  Discard any that are left over from the data set containing more observations.

That's what this program accomplishes ... matching based on whichever data set has fewer observations, and putting those matched observations into PAIRS.  Discarding any that are left over without a match, putting them into ODDBALLS.

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 166 views
  • 2 likes
  • 3 in conversation