DATA Step, Macro, Functions and more

Using in= while setting 2 datasets

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Using in= while setting 2 datasets

I have used the following code:

 

data people;
input obs name$;
cards;
1 Frank
2 Joan
3 Sui
4 Jose
5 Burt
6 Kelly
7 Juan
;
run;
data money;
input name$ sal;
cards;
Frank 100
Joan 210
Sui 250
Jose 330
;
run;

data work.empsalary;
set work.people (in = inemp) work.money (in = insal);
if insal and inemp;
run;

How can the observations be 0 when there are common variables in both sets. can anyone explain it to me.

Thanks.


Accepted Solutions
Solution
‎09-08-2017 10:37 AM
Regular Contributor
Posts: 227

Re: Using in= while setting 2 datasets

data people;
input obs name$;
cards;
1 Frank
2 Joan
3 Sui
4 Jose
5 Burt
6 Kelly
7 Juan
;
run;
proc sort data = &syslast;by name;

PROC sql; describe table &syslast;quit;
data money;
input name$ sal;
cards;
Frank 100
Joan 210
Sui 250
Jose 330
;
run;
proc sort data = &syslast;by name;

PROC sql; describe table &syslast;quit;

data work.empsalary;
merge work.people (in = inemp) work.money (in = insal);
by name;
if insal and inemp then output;
run;
PROC sql; describe table &syslast;quit;
proc print;
run;

the set statement stacks the output

 

use merge+by instead, which requires data attribute says it is sorted,

even though the data in alpha order by name.

 

hth

Ron Fehd  data structure mostly, sometimes algorithm maven

 

View solution in original post


All Replies
Super User
Super User
Posts: 9,211

Re: Using in= while setting 2 datasets

You are setting the data work.people first then afterwards money, therefore the logic if insal and inemp can never be true as both rows never appear on the same row.  Perhaps you meant to merge the two datasets:

data work.empsalary;
  merge work.people (in=inemp) work.money (in=insal);
by name; if insal and inemp; run;

Note, both have to be sorted by name first. 

Solution
‎09-08-2017 10:37 AM
Regular Contributor
Posts: 227

Re: Using in= while setting 2 datasets

data people;
input obs name$;
cards;
1 Frank
2 Joan
3 Sui
4 Jose
5 Burt
6 Kelly
7 Juan
;
run;
proc sort data = &syslast;by name;

PROC sql; describe table &syslast;quit;
data money;
input name$ sal;
cards;
Frank 100
Joan 210
Sui 250
Jose 330
;
run;
proc sort data = &syslast;by name;

PROC sql; describe table &syslast;quit;

data work.empsalary;
merge work.people (in = inemp) work.money (in = insal);
by name;
if insal and inemp then output;
run;
PROC sql; describe table &syslast;quit;
proc print;
run;

the set statement stacks the output

 

use merge+by instead, which requires data attribute says it is sorted,

even though the data in alpha order by name.

 

hth

Ron Fehd  data structure mostly, sometimes algorithm maven

 

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 104 views
  • 0 likes
  • 3 in conversation