BookmarkSubscribeRSS Feed
Dinkepile
Obsidian | Level 7
Good day

Please assist I am trying to concatenate 4 variables but if the values are duplicates I will like to output just a single value.

I have given an example below:


Var1 Var2 Var3 Var4
IN4 IN4 IN4 IN4


Concatenate:
New_Var = var1||"/"||var2||"/"||var3||"/"||var4

The expected value of variable New_Var should be IN4 not IN4/IN4/IN4/IN4

Thank you
Kind Regards
Dinkepile
1 REPLY 1
ballardw
Super User

No data so untested;

 

Data want;
   set have;
   array values (*) var1-var4;
   length new_var $ 16 ; /* this needs to large enough to accept the longest possible value*/
   do i=1 to dim(values);
      if indexw(new_var,values[i],'/')= 0 then new_var= catx('/',new_var,values[i]);
   end;
   drop i;
run;

This searches the value of the new_var for each of the variables and if it is not currently present then concatenates.

If your variables actually have different names then the order they appear on the Array statement is the order they are processed.

If the values vary by case "In4" "in4" "iN4" all of these would appear. So you may need to clean data first if spelling or case differences may occur.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 980 views
  • 0 likes
  • 2 in conversation