BookmarkSubscribeRSS Feed
Almoha
Calcite | Level 5

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

 

5 REPLIES 5
art297
Opal | Level 21

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

novinosrin
Tourmaline | Level 20

/*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

thomp7050
Pyrite | Level 9

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;

PGStats
Opal | Level 21
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;
PG
Ksharp
Super User
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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 834 views
  • 1 like
  • 6 in conversation