If the ID from data set 1 exists in dataset 2, I would like to create a new variable called flag in dataset 2 and it =1 and =0 otherwise. This is shown below. I tried using merge, but couldn't find a good way to do this. any help would be great.
And if performance is of importance, use a data step hash:
data want; set data2; if _n_ = 1 then do; declare hash d1 (dataset:"data1"); d1.definekey("id"); d1.definedone(); end; flag = (d1.check() = 0); run;
One more way, but definitely slower:
data want; merge one(in=a) two(in=b); by id; if a and b then flag=1; else flag=0; if y1=. or y2=. or y3=. then delete; keep id y1 y2 y3 flag; run;
How you handle the missings after the merge is dependent on your ultimate objective. Here I deleted any record missing at least one of y1-y3. I could understand making this different if the criteria for keeping a record was something other than at least one of the variables in dataset 2 is missing.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.