We have two datasets:
/* dataset A */
Period num_1 num_2 num_3
4/2001 - 6/2001 62 14 35
/* dataset B */
Time_Period
1/2001 - 3/2001
4/2001 - 6/2001
7/2001 - 9/2001
10/2001 - 12/2001
The output dataset would look like:
/* dataset want */
Time_Period num_1 num_2 num_3
1/2001 - 3/2001 0 0 0
4/2001 - 6/2001 62 14 35
7/2001 - 9/2001 0 0 0
10/2001 - 12/2001 0 0 0
Not sure how to achieve that? maybe merge or join?
Yes, a merge is what you want (or SQL, but then you will have issues on the output dataset's sort order). Note that if there is no match, you'll want to set num_1-num_3 to 0; otherwise they'll be null.
data want;
merge dataset_a(in=in_a rename=period=time_period)
dataset_b;
by time_period;
if not in_a then do;
num_1 = 0;
num_2 = 0;
num_3 = 0;
end;
run;
Yes, a merge is what you want (or SQL, but then you will have issues on the output dataset's sort order). Note that if there is no match, you'll want to set num_1-num_3 to 0; otherwise they'll be null.
data want;
merge dataset_a(in=in_a rename=period=time_period)
dataset_b;
by time_period;
if not in_a then do;
num_1 = 0;
num_2 = 0;
num_3 = 0;
end;
run;
A SQL left join would do the trick equally well. It can also be contructed in EG using the query builder.
proc sql;
select b.time_period, coalesce(a.num_1, 0), coalesce(a.num_2, 0), coalesce(a.num_3, 0)
from b left join a
on a.time_period=b.time_period;
quit;
Add an ORDER BY clause if you need the results sorted.
Hope this helps,
- Jan.
For consideration
data have; length Time_Period $ 18.; Time_Period ="7/2001 - 9/2001";output; Time_Period ="1/2001 - 3/2001";output; Time_Period ="5/2002 - 6/2002";output; Time_Period ="4/2001 - 6/2001";output; Time_Period ="10/2001 - 12/2001";output; ; run; proc sql; create table want as select * from have order by input(scan(time_period,1,' '),anydtdtm32.) ; quit;
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.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.