BookmarkSubscribeRSS Feed
SilverStation21
Calcite | Level 5

Hi everyone, i'm having a little trouble trying to solve a particular case.

 

In my original table i have 167 different columns, each one with every case possible., some are empty, some are filled, and some have the same data the other columns have.

 

What I am trying to do is create a new column (Final_VAR), that concatenates all the non-empty and only distinct variables!

Here is na example of what I have today:

 

Id  |  Var_1  |  Var_2  |  Var_3  |  Var_4  |

1   | text_1   | text_2  | text_3   | text_4  |

2   | text_2   | text_2  | text_2   | text_2  |

3   | text_1   | text_3  |              |             |

4   | text_3   | text_3  |              |             |

5   | text_4   | text_5  | text_4   | text_5  |

 

The result should be like this:

 

Id | Final_Var

1  | text_1 - text_2 - text_3 - text_4

2  | text_2

3  | text_1 - text_3

4  | text_3

5  | text_4 - text_5

 

I've tried already using CATX, CAT, and CATT, and useid options to remove the blank spaces, but still can't figure out this case, can someone please help me?

 

P.S. Sorry about my english, i'm from Brazil!

1 REPLY 1
novinosrin
Tourmaline | Level 20
data have;
infile cards dlm='|' ;
input Id  (Var1-Var4) ($);
cards;
1   | text_1   | text_2  | text_3   | text_4  |
2   | text_2   | text_2  | text_2   | text_2  |
3   | text_1   | text_3  |              |             |
4   | text_3   | text_3  |              |             |
5   | text_4   | text_5  | text_4   | text_5  |
;

data want;
 set have;
 array v var1--var4;
 length final_var $200;
 do over v;
  if index(final_var,strip(v)) then continue;
  final_var=catx('-',final_var,v);
 end;
 drop var1-var4;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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