DATA Step, Macro, Functions and more

Create a new variable by concatenating other non-missing variables

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Create a new variable by concatenating other non-missing variables

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:

ObsBarrier1Barrier2Barrier3Barrier4Barrier5Barrier6Barrier7
1ChildFundingTitle IV-E
2FundingCourtsTeam
3PolicyTeamOther
4ChildCourts
5FundingPolicy
6ChildCourtsTitle IV-EOther

Desired new variable:

ObsBarriers
1Child, Funding, Title IV-E
2Funding, Courts, Team
3Policy, Team, Other
4Child, Courts
5Funding, Policy
6Child, Courts, Title IV-E, Other

Accepted Solutions
Solution
‎02-28-2014 05:52 PM
Super User
Posts: 17,775

Re: Create a new variable by concatenating other non-missing variables

call catx function

View solution in original post


All Replies
Solution
‎02-28-2014 05:52 PM
Super User
Posts: 17,775

Re: Create a new variable by concatenating other non-missing variables

call catx function

New Contributor
Posts: 4

Re: Create a new variable by concatenating other non-missing variables

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! Smiley Happy

(That's 2 for 2 today, Reeza.)

Super User
Posts: 10,476

Re: Create a new variable by concatenating other non-missing variables

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 223 views
  • 3 likes
  • 3 in conversation