Hello All
i have the following
col col1
PARAM PARAMCD
PARACD PARAM
AVAL AVALC
AVALC AVAL
AVISIT AVISTIN
AVISITN AVISIT
AVISIT AVAL
AVAL PARAMCD
I should be able to compare col and col1 variables and create a variable output as follows
i.e extract “CD” is it PARAM vs PARAMCD
extract “C” if it is AVAL vs AVALC
extract “N” if it is AVISIT vs AVISITN
col col1 output
PARAM PARAMCD CD
PARAMCD PARAM CD
AVAL AVALC C
AVALC AVAL C
AVISIT AVISTIN N
AVISITN AVISIT N
AVISIT AVAL
AVAL PARAMCD
Here is one way:
data have; input (col col1) ($); cards; PARAM PARAMCD PARAMCD PARAM AVAL AVALC AVALC AVAL AVISIT AVISITN AVISITN AVISIT AVISIT AVAL AVAL PARAMCD ; data want; set have; array cols(*) col col1; call sortc(of cols(*)); if cols(1) eq 'PARAM' and cols(2) eq 'PARAMCD' then output="CD"; else if cols(1) eq 'AVAL' and cols(2) eq 'AVALC' then output="C"; else if cols(1) eq 'AVISIT' and cols(2) eq 'AVISITN' then output="N"; run;
Art, CEO, AnalystFinder.com
/*untested*/
data want;
set have;
temp=compare(col1,col2);
if temp<0 then find=substr(col2,abs(temp));
else if temp>0 then find=substr(col1,temp);
drop temp;
run;
Regards,
Naveen Srinivasan
Yet another way:
DATA MYDATA;
LENGTH COL $7. COL1 $7.;
INPUT COL COL1;
CARDS;
PARAM PARAMCD
PARACD PARAM
AVAL AVALC
AVALC AVAL
AVISIT AVISTIN
AVISITN AVISIT
AVISIT AVAL
AVAL PARAMCD
;
RUN;
DATA MYDATA;
SET MYDATA;
LENGTH REMAINDER $ 15;
IF INDEX(TRIM(COL1), TRIM(COL)) > 0 THEN DO;
REMAINDER = TRANWRD(TRIM(COL1), TRIM(COL), ' ');
END;
RUN;
data have;
input (col col1) ($);
cards;
PARAM PARAMCD
PARAMCD PARAM
AVAL AVALC
AVALC AVAL
AVISIT AVISITN
AVISITN AVISIT
AVISIT AVAL
AVAL PARAMCD
;
data want;
set have;
if trim(col) =: trim(col1) then
if length(col) < length(col1) then
output = substr(col1, length(col)+1);
else output = substr(col, length(col1)+1);
run;
proc print; run;
data have;
input (col col1) ($);
cards;
PARAM PARAMCD
PARAMCD PARAM
AVAL AVALC
AVALC AVAL
AVISIT AVISITN
AVISITN AVISIT
AVISIT AVAL
AVAL PARAMCD
;
run;
data want;
set have;
x=abs(compare(col,col1));
len1=length(col);
len2=length(col1);
if x gt min(len1,len2) then do;
if x le len1 then output=substr(col,x);
else output=substr(col1,x);
end;
run;
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.