10-27-2017 08:01 AM
This should be pretty simple but I'm having difficulty figuring it out. I'm trying to combine values from multiple observations into one field. From the first table below to the 2nd. Note that in some instances 'PART1' could have only one record, sometimes 2 and sometimes 3 so the code would have to be written to accommodate this. I've shown it with PART1 having 3 records below.
Any help would be appreciated!!!
10-27-2017 08:20 AM
First of all, you have to create a new variable with sufficient length.
In a data step, retain this new variable.
Use by part_name;
At first.part_name, set the new variable to empty
Concatenate by using newvar = catx('/',trim(newvar),claim_cat);
At last.part_name, output
That should do it.
10-27-2017 08:41 AM
Hi. Thanks for your input. I'm trying this but I just keep getting the same value in the new field as in the old...Here's the code (with some variable name changes to suit my actual data). I must be doing something wrong...
|EGR COOLER, REPLACEMENT KIT||EXT|
|EGR COOLER, REPLACEMENT KIT||STD|
|EGR COOLER, REPLACEMENT KIT||PURCH|
length concat $30.;
if first.PART_NAME then concat="";
if last.PART_NAME then output;
|EGR COOLER, REPLACEMENT KIT||PURCH||PURCH|
10-27-2017 09:03 AM
You need to retain the new variable:
data testdave1; set testdave; retain concat; by PART_NAME; length concat $30.; if first.PART_NAME then concat = ""; concat = catx('/',concat,claim_cat); if last.PART_NAME then output; drop claim_cat; run;
The trim() functions are actually not necessary, as catx() does that automatically.