Hi folks,
Probably a common problem, and I'm just blanking on a solution that doesn't require 5,040 lines of code...
I have seven variables, of which any number of them can be populated or missing. In any combination. And I'd like to concatenate them into a single variables that displays the populated values as a list separated by commas. But obviously I don't want extraneous commas, so I only want the new variable to include non-missing values. Any thoughts on how to do this?
Original data:
Obs | Barrier1 | Barrier2 | Barrier3 | Barrier4 | Barrier5 | Barrier6 | Barrier7 |
---|---|---|---|---|---|---|---|
1 | Child | Funding | Title IV-E | ||||
2 | Funding | Courts | Team | ||||
3 | Policy | Team | Other | ||||
4 | Child | Courts | |||||
5 | Funding | Policy | |||||
6 | Child | Courts | Title IV-E | Other |
Desired new variable:
Obs | Barriers |
---|---|
1 | Child, Funding, Title IV-E |
2 | Funding, Courts, Team |
3 | Policy, Team, Other |
4 | Child, Courts |
5 | Funding, Policy |
6 | Child, Courts, Title IV-E, Other |
call catx function
call catx function
Oh, duh. It really is a Friday isn't it... where is my brain? I'm so used to using the long-hand version with the concatenation operator I completely forgot about the catx function. Thanks!
(That's 2 for 2 today, Reeza.)
I would start with CATX function in a data step
data want;
set have;
length combinedBarriers $ <big enough integer to store longest combination>;
combinedbarriers = catx(',' of Barrier1-Barrier7);
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.