DATA Step, Macro, Functions and more

How do i get values based on priority

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

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: 10,516

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.

 

View solution in original post


All Replies
Super User
Posts: 10,516

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.

 

Occasional Contributor
Posts: 8

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

Trusted Advisor
Posts: 1,392

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? 

 

 

 

Occasional Contributor
Posts: 8

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.

 

 

Thanks in advance

Solution
‎06-06-2017 05:01 PM
Super User
Posts: 10,516

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 128 views
  • 2 likes
  • 3 in conversation