I was wondering if anyone knows how I can merge two data sets conditionally while also retaining all observations, regardless of whether they merged. I have two data sets, each is unique by year and individ_id_num. Each data set contains multiple years of data (2000-2020), but I only want to merge for the year=2018. I seem to have achieved this with the code below, but my final data set only contains observations where year=2018. I want to retains all observations (in1) regardless of whether it merged to an observation in the merging data set. Any ideas?
data want; merge have1(where=(year=2018) in=in1) have2(where=(year=2018) in=in2); by indiv_id_num; if in1; if in1 and in2 then merge_18=1; else merge_18 = 0; run;
What is wrong with the code you show?
The modification to the code should be obvious.
The code you showed uses this fragment of code:
have1(where=(year=2018))
If you want to change the code to use all years, you modify the above code to ...
The where= dataset options prevent any years other than 2018 from processing in your data step, so you need to remove those.
You will need to merge by indiv_id_num and year, and set your final variable only when year = 2018.
To provide you with (tested) code, we need examples for have1 and have2, and the expected outcome from those examples. Provide those examples as data steps with datalines, so we can easily create your datasets in our environments.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.