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.
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.
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.
Thank you. It's scary to delete stuff, but that's why we have backups.
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.
I transposed because I need to get from multiple rows per respondent to one row per respondent.
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;
Great idea. Thanks again.
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.
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.