Hi ,
I have a dataset like which is shown below . I need to check within the same item group whether the item values differ for item number . I need to write an automated code . Can anyone help me
Itemgroup | Item_number | item value |
5 | 1 | 1.043 |
5 | 1 | 0.875 |
5 | 2 | 0.936 |
5 | 2 | 0.567 |
6 | 1 | 1.567 |
6 | 1 | 1.258 |
6 | 2 | 1.567 |
6 | 2 | 1.258 |
Thanks
This could give a start.
data have;
infile cards missover;
input Itemgroup Item_number item_value;
cards;
5 1 1.043
5 1 0.875
5 2 0.936
5 2 0.567
6 1 1.567
6 1 1.258
6 2 1.567
6 2 1.258
;
run;
data A;
set have;
if Item_number=1;
rename item_value=A;
run;
data B;
set have;
if Item_number=2;
rename item_value=B;
run;
data A_B;
merge A B(drop=Item_number);
A_B=A-B;
drop A B;
run;
data want;
merge have A B A_B;
by Itemgroup Item_number;
run;
What do you want for output? A data set or a report? Do you need to know how many different values or just that there is at least one?
Your example data implies that you have multiple value for every one.
Several different procedures will provide different types of reports.
Here's on approach if all you need is to know that there are differences:
Proc tabulate data=have;
class itemgroup item_number;
var Item_value;
table itemgroup * item_number,
item_value*range="0 indicates no difference across item numbers";
run;
Hi , please try the below code, the variable diff will have a value of greater than 1 if the values dont differ within the group and it will have 1 if the values differ within the group. Hope it helps
data have;
infile cards missover;
input Itemgroup Item_number item_value;
cards;
5 1 1.043
5 1 0.875
5 2 0.936
5 2 0.567
6 1 1.567
6 1 1.258
6 2 1.567
6 2 1.258
;
proc sort data=have;
by Itemgroup item_value;
run;
data want;
do until(last.item_value);
set have;
by Itemgroup item_value;
if first.item_value then diff=1;
else diff+1;
end;
do until(last.item_value);
set have;
by Itemgroup item_value;
output;
end;
run;
Hi ,
I am expecting an output like below but i am unable to do A-B but i need to cretae column A-B .
zero output is because 1.567 for item#1 and item#2 is same
Itemgroup | Item_number | item value | A | B | A-B |
5 | 1 | 1.043 | 1.043 | . | 0.107 |
5 | 1 | 0.875 | 0.875 | . | 0.308 |
5 | 2 | 0.936 | 0.936 | ||
5 | 2 | 0.567 | 0.567 | ||
6 | 1 | 1.567 | 1.567 | . | 0 |
6 | 1 | 1.258 | 1.258 | . | 0 |
6 | 2 | 1.567 | 1.567 | ||
6 | 2 | 1.258 | 1.258 |
You original post implied that you were comparing within item_number which would make sense given the data shown. Your "item_number" is appraently and order variable (if I understand the display) and make much less sense given that you apparently are assigning the same order to two different series of values.
I would expect there to be a variable that indicates that 1.043 in Item group 5 should be compared to 0.936 within group 5.
Do any of your Itemgroups have more than 4 rows with this 1-1-2-2 assignment? The data you have is likely going to need an additional variable added to control the comparisons and complete descriptions of order combination would be needed to get that.
Hi ,
Yes I have a column for each item group which is
Itemgroup | Item_number | item value | A | B | A-B | Key |
5 | 1 | 1.043 | 1.043 | . | 0.107 | +009 |
5 | 1 | 0.875 | 0.875 | . | 0.308 | +019 |
5 | 2 | 0.936 | 0.936 | +009 | ||
5 | 2 | 0.567 | 0.567 | +019 | ||
6 | 1 | 1.567 | 1.567 | . | 0 | +014 |
6 | 1 | 1.258 | 1.258 | . | 0 | +015 |
6 | 2 | 1.567 | 1.567 | +014 | ||
6 | 2 | 1.258 | 1.258 | +015 |
This could give a start.
data have;
infile cards missover;
input Itemgroup Item_number item_value;
cards;
5 1 1.043
5 1 0.875
5 2 0.936
5 2 0.567
6 1 1.567
6 1 1.258
6 2 1.567
6 2 1.258
;
run;
data A;
set have;
if Item_number=1;
rename item_value=A;
run;
data B;
set have;
if Item_number=2;
rename item_value=B;
run;
data A_B;
merge A B(drop=Item_number);
A_B=A-B;
drop A B;
run;
data want;
merge have A B A_B;
by Itemgroup Item_number;
run;
Itemgroup | Item_number | item_value | diff |
5 | 2 | 0.567 | 1 |
5 | 1 | 0.875 | 1 |
5 | 2 | 0.936 | 1 |
5 | 1 | 1.043 | 1 |
6 | 1 | 1.258 | 2 |
6 | 2 | 1.258 | 2 |
6 | 1 | 1.567 | 2 |
6 | 2 | 1.567 | 2 |
but i need within the same item group (for eg 5) whether all values of item_value are same for two diff item numbers ( 1 and 2 ) like for item group 6 when u look the values are same for item #1 and item#2 so i want to check using code
Thanks
data have; infile cards missover; input Itemgroup Item_number item_value; cards; 5 1 1.043 5 1 0.875 5 2 0.936 5 2 0.567 6 1 1.567 6 1 1.258 6 2 1.567 6 2 1.258 ; run; proc sort data=have; by Itemgroup item_value; run; data want; set have; by Itemgroup item_value; diff=not (first.item_value and last.item_value) +1; run;
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!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.