I would like to create a new dataset with only 1 row for each ID. I would like the first non-null value from the "field" variable to be placed into a new variable called "Animal". I have included an example of my orginal dataset and my desired dataset below. What would be the best approach to create the desired dataset? Thanks in advance for any help you can provide.
Assuming you want to drop IDs that have no animal :
proc sort data=original(where=(field is not missing)) out=animals equals /* maintain original order within by-groups */; by ID; run; data desired; set animals; by ID; if first.ID; rename field=Animal; run;
If you have another "order" related variable such as date then sort by id and that other variable and use First.processing. If your original data order is important but you don't have such a variable then add one:
set have (where= (field ne ''));
Proc sort data=temp ;
by Id order;
And First. processing
by id order;
keep id animal;
Updated my answer for brevity (as in PG's and Ksharp's posts) and included a second option which would be helpful for very large datasets:
*Sample data; data original; infile datalines dlm="|" dsd; input ID field $ @@; datalines; 1||2|dog|3||2||1|cat|3|dog|1|cat|1||4|hamster|2|dog ; run; *Approach one: sorting and deduping by ID preserving the relative order; proc sort data = original (where=(field is not missing)) out = result (rename=(field=animal)) EQUALS NODUPKEY; by ID; run; *Aproach two: Using a hash table to pass once thru the data without having to sort beforehand; data result2; set original; where ~missing(field); if _N_ = 1 then do; declare hash hAnimals(); _rc = hAnimals.DefineKey('ID'); _rc = hAnimals.DefineData('field'); _rc = hAnimals.DefineDone(); drop _rc; end; if hAnimals.find() then do; _rc = hAnimals.add(); output; end; rename field=animal; run;
Compress PG's code into one proc :
data original; infile datalines dlm="|" dsd; input ID field $ @@; datalines; 1||2|dog|3||2||1|cat|3|dog|1|cat|1||4|hamster|2|dog ; run; proc sort data=original(where=(field is not missing)) out=want nodupkey; by ID; run;
Me too. I like to explicitly request the options that I need, even if they are the default, just out of precaution. If the NOSORTEQUALS system option was turned on for any reason, that would change the default of the sort procedure. It also makes your intent more clear in the code. Agree 100%
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.