Help using Base SAS procedures

Base SAS Code

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 125
Accepted Solution

Base SAS Code

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


Accepted Solutions
Solution
‎06-08-2016 10:54 AM
Super User
Posts: 10,035

Re: Base SAS Code

Posted in reply to chennupriya

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


All Replies
Super User
Posts: 11,343

Re: Base SAS Code

Posted in reply to chennupriya

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;

Trusted Advisor
Posts: 1,137

Re: Base SAS Code

Posted in reply to chennupriya

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
Frequent Contributor
Posts: 125

Re: Base SAS Code

Posted in reply to Jagadishkatam

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  
Super User
Posts: 11,343

Re: Base SAS Code

Posted in reply to chennupriya

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.

Frequent Contributor
Posts: 125

Re: Base SAS Code

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
Solution
‎06-08-2016 10:54 AM
Super User
Posts: 10,035

Re: Base SAS Code

Posted in reply to chennupriya

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;
Frequent Contributor
Posts: 125

Re: Base SAS Code

[ Edited ]
Posted in reply to Jagadishkatam
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

Super User
Posts: 10,035

Re: Base SAS Code

Posted in reply to chennupriya
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;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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