## Help with compare variable1 value with numeric part of name of variable2

Solved
Super Contributor
Posts: 506

# Help with compare variable1 value with numeric part of name of variable2

Hi Everyone,

I have the following data with Window and code1 code2... code100 variables.

For a given record, I compare the value in Window variable with the numerical part (the last 1 or 2 character) of the name of code. If they are the same the new Value variable will be equal to the value of that code.

Basically it should be like:

if window<=9 and window=last 1  character of variable (code1) then value=code1;

else if window<=9 and window=last 1 character of variable (code2) then value=code2

else if window>9  and window=last 2 character of variable (code11) then value=code11; *for 2 digit number;

...

So the value should be like:

1

.

4

.

6

.

.

6

.

Thank you so much.

HHC

data have;

input window code1 code2 code11;

datalines;

2 2 1 3

3 3 3 4

1 4 5 6

4 6 7 8

2 8 6 3

6 9 5 7

7 1 4 5

11 4 5 6

10 5 6 7

;run;

Accepted Solutions
Solution
‎07-29-2015 08:30 AM
Posts: 3,852

## Re: Help with compare variable1 value with numeric part of name of variable2

Why not create an ARRAY and use window as the subscript?

data have;
input window code1 code2 code11;
datalines;
2 2 1 3
3 3 3 4
1 4 5 6
4 6 7 8
2 8 6 3
6 9 5 7
7 1 4 5
11 4 5 6
10 5 6 7
;;;;
run;
data window;
set have;
keep window code1--code11 value;
array code[11];
value = code[window];

run;
proc print;

run;

All Replies
Super User
Posts: 9,599

## Re: Help with compare variable1 value with numeric part of name of variable2

The more I think about it the less I like it.  What is code, how did it get onto the data?  Why does the precision change for variables > 9?  Why does code and window not match one-one?   Too many questions really.

Well,

data want;

set have;

if window <= 9 and window=input(substr(strip(reverse(put(code1,best.))),1,1),best.) then value=code1;

else if window <= 9 and window=input(substr(strip(reverse(put(code2,best.))),1,1),best.) then value=code1;

...

run;

Solution
‎07-29-2015 08:30 AM
Posts: 3,852

## Re: Help with compare variable1 value with numeric part of name of variable2

Why not create an ARRAY and use window as the subscript?

data have;
input window code1 code2 code11;
datalines;
2 2 1 3
3 3 3 4
1 4 5 6
4 6 7 8
2 8 6 3
6 9 5 7
7 1 4 5
11 4 5 6
10 5 6 7
;;;;
run;
data window;
set have;
keep window code1--code11 value;
array code[11];
value = code[window];

run;
proc print;

run;

Super Contributor
Posts: 506

## Re: Help with compare variable1 value with numeric part of name of variable2

Thank you, Data_Null,

It work perfectly.

🔒 This topic is solved and locked.