BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
UMAnalyst
Obsidian | Level 7

I transposed data from long to wide and now I need to compare multiple strings (up to four string vars) to determine similitude. I need to concatenate  only the unequal strings.

 

example:

ID       comment1              comment2             comment3       Comment4              RESULT

1001   This is some text   This is some text                                                           This is some text

1002   More text like this  More text like this   Not the same   More text like this   More text like this Not the same

 

Currently, only my first if-then is holding, which is "if comment1=comment2 then COMMENT=comment1". I'll spare you the rest, because it's not pretty.

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

SImplest would be to change the variables to eliminate duplicates:

 

array comment {4};

do I=1 to 3;

   do J=i+1 to 4;

      if comment{I} = comment{J} then comment{J}=' ';

   end;

end;

 

Then combine them:

 

result = catx(' ', of comment1-comment4);

 

If it's not acceptable to replace the original COMMENT variables, copy them to new variables first and work on the new variables to get RESULT.

View solution in original post

7 REPLIES 7
Astounding
PROC Star

SImplest would be to change the variables to eliminate duplicates:

 

array comment {4};

do I=1 to 3;

   do J=i+1 to 4;

      if comment{I} = comment{J} then comment{J}=' ';

   end;

end;

 

Then combine them:

 

result = catx(' ', of comment1-comment4);

 

If it's not acceptable to replace the original COMMENT variables, copy them to new variables first and work on the new variables to get RESULT.

UMAnalyst
Obsidian | Level 7

Thank you. It's scary to delete stuff, but that's why we have backups.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why transpose the data in the first place to to this, then code around the fact that its transposed?  Just do your logic on the normalised data.

UMAnalyst
Obsidian | Level 7

I transposed because I need to get from multiple rows per respondent to one row per respondent.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Perhaps you misunderstand, you can process the data either in long or wide format, it makes no difference.  Personally I would use transposed data only for reporting purposes (apart from a few times).  Its just simpler working with normalised data:

data have;
  length comment $50;
  id=1; 
  comment="This is some text"; output;
  comment="This is some text"; output;
  comment=""; output;
  comment="This is some text"; output;
  id=2; 
  comment="More text like this"; output;
  comment="More text like this"; output;
  comment="Not the same"; output;
  comment="More text like this"; output;
run;

proc sort data=have nodupkey;
  by id comment;
run;

data want (keep=id result);
  set have;
  by id;
  retain result;
  length result $2000;
  if first.id then result="";
  result=catx(" ",result,comment);
  if last.id then output;
run;
UMAnalyst
Obsidian | Level 7

Great idea. Thanks again.

pearsoninst
Pyrite | Level 9
you can continue comment1=comment2 and
comment3=comment4 and so on.. as you have only 4 Variables and if you do not understand Do Loops.How ever DL is actually a permanent solution

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 7 replies
  • 1956 views
  • 2 likes
  • 4 in conversation