Solved
Contributor
Posts: 20

# How do i get values based on priority

I have data like this

id           value

1              a,b,c

2               e,f,c

3               d,e,f

i want output like

id           value          max_value

1              a,b,c             a

2               e,f,c              c

3               d,e,f              d

considering the order of priority from a-z  and a being highest.

Thanks,

Jay

Accepted Solutions
Solution
‎06-06-2017 05:01 PM
Super User
Posts: 13,583

## Re: How do i get values based on priority

[ Edited ]

jb9977 wrote:

Thanks for such a quick reply.. My values are in order and i have to define my priority. How can i do that ?

My values are IL, IN, SA, SP

SA priority 1

SP priority 2

IL priority 3

IN priority 4

Thanks,

Jay

I would use a custom format to create a new character value or an informat if I wanted a numeric:

```proc format library=work;
value \$priority
'SA'='1'
'SP'='2'
'IL'='3'
'IN'='4'
;
invalue priority
'SA'=1
'SP'=2
'IL'=3
'IN'=4
;
run;

data example;
input code \$;
newchar = put(code,\$priority.);
numval = input(code,priority.);
datalines;
SA
SP
IL
IN
;
run;

```

Formats and informats are one very slick reusable way to do lookups. And if you have a data set with the current value and the new value needed you can modify that to use as input to Proc format to make formats and informats.

And how to use with similar data to your previous example:

```data have;
infile datalines truncover;
informat id best5. value \$20.;
input id  value ;
datalines;
1 SP,SA,SP
2 IL,IL,SP
3 IN,SP,IL
3 IN,IN,IL
;
run;

data want;
set have;
max_value= 99;
do i = 1 to (countw(value));
max_value = min(max_value,input(scan(value,i),priority.));
end;
drop i;
run;```

A 99 would mean none of the coded values you have in your list of "priority" values was in the variable Value.

All Replies
Super User
Posts: 13,583

## Re: How do i get values based on priority

assuming your values are actually single lower-case letters than this works for the provided example.

```data have;
infile datalines truncover;
informat id best5. value \$10.;
input id  value ;
datalines;
1 a,b,c
2 e,f,c
3 d,e,f
;
run;

data want;
set have;
max_value= 'z';
do i = 1 to (length(value));
if substr(value,i,1) < max_value and substr(value,i,1) ge 'a' then max_value=substr(value,i,1);
end;
drop i;
run;```

If you ever have blanks in value then you need to define the result desired.

Contributor
Posts: 20

## Re: How do i get values based on priority

Thanks for such a quick reply.. My values are in order and i have to define my priority. How can i do that ?

My values are IL, IN, SA, SP

SA priority 1

SP priority 2

IL priority 3

IN priority 4

Thanks,

Jay

Posts: 1,837

## Re: How do i get values based on priority

Your first post and last post are not consistent,

In the first the values are combinations of one character, like a,b,c or d,e,f etc.

while in the last are probably combinations of two characters each, like:   IL,SP,IN  - is it ?

Does VALUE variable always contains 3 different values?

How many distinct values (like: SP or IL or IN etc.) are in your dataset?

Contributor
Posts: 20

## Re: How do i get values based on priority

My data is combination of two characters and i have 25 distinct values (like: SP or IL or IN etc.) in my data set and my VALUE variable contain  5 different values.

Solution
‎06-06-2017 05:01 PM
Super User
Posts: 13,583

## Re: How do i get values based on priority

[ Edited ]

jb9977 wrote:

Thanks for such a quick reply.. My values are in order and i have to define my priority. How can i do that ?

My values are IL, IN, SA, SP

SA priority 1

SP priority 2

IL priority 3

IN priority 4

Thanks,

Jay

I would use a custom format to create a new character value or an informat if I wanted a numeric:

```proc format library=work;
value \$priority
'SA'='1'
'SP'='2'
'IL'='3'
'IN'='4'
;
invalue priority
'SA'=1
'SP'=2
'IL'=3
'IN'=4
;
run;

data example;
input code \$;
newchar = put(code,\$priority.);
numval = input(code,priority.);
datalines;
SA
SP
IL
IN
;
run;

```

Formats and informats are one very slick reusable way to do lookups. And if you have a data set with the current value and the new value needed you can modify that to use as input to Proc format to make formats and informats.

And how to use with similar data to your previous example:

```data have;
infile datalines truncover;
informat id best5. value \$20.;
input id  value ;
datalines;
1 SP,SA,SP
2 IL,IL,SP
3 IN,SP,IL
3 IN,IN,IL
;
run;

data want;
set have;
max_value= 99;
do i = 1 to (countw(value));
max_value = min(max_value,input(scan(value,i),priority.));
end;
drop i;
run;```

A 99 would mean none of the coded values you have in your list of "priority" values was in the variable Value.

☑ This topic is solved.