BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jb9977
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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

5 REPLIES 5
ballardw
Super User

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.

 

jb9977
Fluorite | Level 6

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

Shmuel
Garnet | Level 18

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? 

 

 

 

jb9977
Fluorite | Level 6

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

ballardw
Super User

@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.

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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