Hello, I would like to ask a question on SET statements when used to combine tables.
I have got two tables which I am trying to combine:
Table Global
| EmployeeID | Name | Location |
| 1 | A | Global |
| 2 | B | Global |
| 3 | C | Global |
Table Local
| EmployeeID | Name | Location | Gender |
| 1 | A | Europe | M |
| 2 | B | Asia | F |
| 3 | C | Africa | M |
I want to use SET statements:
DATA Combined;
SET Global Local;
RUN;
That gives:
| EmployeeID | Name | Location | Gender |
| 1 | A | Global | M |
| 1 | A | Europe | |
| 2 | B | Global | F |
| 2 | B | Asia | |
| 3 | C | Global | M |
| 3 | C | Africa |
What I wanted to find out is if I can get the below results still using SET statements:
| EmployeeID | Name | Location | Gender |
| 1 | A | Global | M |
| 1 | A | Europe | M |
| 2 | B | Global | F |
| 2 | B | Asia | F |
| 3 | C | Global | M |
| 3 | C | Africa | M |
Thanks in advance.
Try this:
data combined;
set
local (rename=(gender=_gender))
global
;
by employeeid;
retain gender;
if first.employeeid then gender = _gender;
drop _gender;
run;
Or, shorter:
data combined;
set
local
global
;
by employeeid;
gender = ifc(first.employeeid,gender,lag(gender));
run;
Try this:
data combined;
set
local (rename=(gender=_gender))
global
;
by employeeid;
retain gender;
if first.employeeid then gender = _gender;
drop _gender;
run;
Or, shorter:
data combined;
set
local
global
;
by employeeid;
gender = ifc(first.employeeid,gender,lag(gender));
run;
Thanks a lot! I learnt something new. I was hoping though not to have to sort the data.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.