Please help me to get the below output from the input data using SAS datastep or proc sql:
Input Data:
ID | Value |
1 | ABC |
1 | Others |
1 | Others |
2 | Others |
2 | Others |
3 | ABC |
3 | ABC |
3 | ABC |
4 | ABC |
4 | Others |
5 | Others |
Output:
ID | Value |
1 | Hybrid |
2 | Others |
3 | ABC |
4 | Hybrid |
5 | Others |
data want;
do until (last.id);
set have;
by id;
if value ne lag(value) and not first.id then flag = 1;
end;
if flag then value = "Hybrid";
drop flag;
run;
I have a good idea of how you might do this, but you have not specified the rule you wish to use to generate the displayed output from the sample input.
Please state the rule you want to apply - which of course is the first step in generating appropriate SAS code.
So basically, if common value is there related to an ID then that value should be there with distinct ID in output table, similarly if different values are there for an ID then in output Hybrid should be published with that distinct ID.
@Vimal1010 wrote:
So basically, if common value is there related to an ID then that value should be there with distinct ID in output table, similarly if different values are there for an ID then in output Hybrid should be published with that distinct ID.
Thank you. I think this code does what you want:
data want (drop=_:);
set have;
by id value notsorted;
if first.id then _nvalue=1;
else if first.value then _nvalue+1;
if last.id;
if _nvalue>1 then value='Hybrid';
run;
Thank you!
data want;
do until (last.id);
set have;
by id;
if value ne lag(value) and not first.id then flag = 1;
end;
if flag then value = "Hybrid";
drop flag;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.