## Datastep logic

Occasional Contributor
Posts: 12

# Datastep logic

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

PROC Star
Posts: 8,163

## Re: Datastep logic

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

PROC Star
Posts: 1,778

## Re: Datastep logic

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

Frequent Contributor
Posts: 93

## Re: Datastep logic

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;

Posts: 5,523

## Re: Datastep logic

``````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
Super User
Posts: 10,770

## Re: Datastep logic

``````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;``````
Discussion stats
• 5 replies
• 143 views
• 1 like
• 6 in conversation