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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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;

View solution in original post

8 REPLIES 8
ballardw
Super User

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;

Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
chennupriya
Quartz | Level 8

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  
ballardw
Super User

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.

chennupriya
Quartz | Level 8

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
Ksharp
Super User

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;
chennupriya
Quartz | Level 8
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

Ksharp
Super User
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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1439 views
  • 1 like
  • 4 in conversation