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
@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.
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.
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
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?
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
@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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.