BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello

I want to create a new column that concatenate reasons.

My question-

In the resulted concatenated column there is sign "||" at the end and I dont want to have it.

What is the way to modify the code to prevent "||" sign at end of the string?

Please note that I want to have "||" between reasons but not in end. 

Data have;
input ID  X1 X2 X3 ;
cards;
1 1 0 0
2 0 0 0
3 1 1 0
4 0 0 1
5 0 1 0
6 1 1 1
7 0 0 0
8 0 1 1
9 0 1 0
10 0 0 1
;
Run;

Data want;
length Reasons  $100.;
set have;
Reasons='';
If X1=1 then Reasons=compress(Reasons||'Health_issue||');
If X2=1 then Reasons=compress(Reasons||'Wealth_issue||');
If X3=1 then Reasons=compress(Reasons||'Other_issue||');
IF X1=0 AND X2=0 AND X3=0 then Reasons='No_Reasons';
Run;
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
Opal | Level 21

Here's a way to get rid of the extra ||.  Switch from COMPRESS to CATX:


If X1=1 then Reasons=catx('||', Reasons, 'Health_issue');
If X2=1 then Reasons=catx('||', Reasons, 'Wealth_issue');
If X3=1 then Reasons=catx('||', Reasons, 'Other_issue');
IF X1=0 AND X2=0 AND X3=0 then Reasons='No_Reasons';

View solution in original post

1 REPLY 1
Astounding
Opal | Level 21

Here's a way to get rid of the extra ||.  Switch from COMPRESS to CATX:


If X1=1 then Reasons=catx('||', Reasons, 'Health_issue');
If X2=1 then Reasons=catx('||', Reasons, 'Wealth_issue');
If X3=1 then Reasons=catx('||', Reasons, 'Other_issue');
IF X1=0 AND X2=0 AND X3=0 then Reasons='No_Reasons';

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 1 reply
  • 80 views
  • 1 like
  • 2 in conversation