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

Hi,

I have a dataset  "Reproduction" with data from  artificial inseminations(AI) in animals during 1 oestrous period.

 

Data Reproduction

ID      AI1     AI2    AI3    AI4   AIn

1       B2      B2     B1     B2   4

2       B1      B1     B1     .       3

3       B1      B3     B3     B3   4

4       B2      B2     B2     .       3

5       B1      B1       .       .       2 

...(5400  obs)

The values "B1, B2 or B3" are  male ID.  I want to know if  inseminations are with same male (event '1') or different male (event '0'), like this:

Data Reproduction

ID      AI1     AI2    AI3    AI4   AIn   Want

1       B2      B2     B1     B2   4        0

2       B1      B1     B1     .       3        1

3       B1      B3     B3     B3   4        0

4       B2      B2     B2     .       3        1

5       B1      B1       .       .       2        1

...(5400  obs)

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Just test if there is ever a value that does not match the first one.

data want;
  set have ;
  array ai ai1-ai4 ;
  do index=1 to ain until(want=0);
    want = ai[1] = ai[index] ;
  end;
  drop index;
run;

View solution in original post

9 REPLIES 9
Tom
Super User Tom
Super User

Just test if there is ever a value that does not match the first one.

data want;
  set have ;
  array ai ai1-ai4 ;
  do index=1 to ain until(want=0);
    want = ai[1] = ai[index] ;
  end;
  drop index;
run;
Ksharp
Super User

Tom,

What if the first variable is missing ?

 

data have;
input ID      AI1  $   AI2  $  AI3 $   AI4  $ AIn;
cards;
1       B2      B2     B1     B2   4
2       B1      B1     B1     .       3
3       B1      B3     B3     B3   4
4       B2      B2     B2     .       3
5       .      B1       .       .       2 
;

Ksharp_0-1623154755022.png

 

Tom
Super User Tom
Super User

I doubt that is what they actually have, why have the counter variable if missing values are not at the end?

 

But it is not hard to adjust for missing values.

data want;
  set have ;
  array ai ai1-ai4 ;
  want=1;
  do index=1 to ain while(missing(ai[index])); 
  end;
  do index2=index+1 to dim(ai) until(want=0);
    if not missing(ai[index2]) then want = ai[index] = ai[index2] ;
  end;
  drop index index2;
run;
Ksharp
Super User

Tom,

What if all the variable were missing ?

 

data have;
input ID      AI1  $   AI2  $  AI3 $   AI4  $ AIn;
cards;
1       B2      B2     B1     B2   4
2       B1      B1     B1     .       3
3       B1      B3     B3     B3   4
4       B2      B2     B2     .       3
5       .      .       .       .       2 
;
Tom
Super User Tom
Super User

What is the right answer for a subject with all missing values?

That case is not hard to test for.

array ai ai1-ai4 ;
if cmiss(of ai[*]) = dim(ai) then ....
Ksharp
Super User

Tom,

I think should be zero.

 

Ksharp_0-1623245959473.png

 

CHGMartinez
Fluorite | Level 6
Interesting question... Thank you so much Tom and Ksharp

Actually missing values are just at the end.
Ksharp
Super User
data have;
input ID      AI1  $   AI2  $  AI3 $   AI4  $ AIn;
cards;
1       B2      B2     B1     B2   4
2       B1      B1     B1     .       3
3       B1      B3     B3     B3   4
4       B2      B2     B2     .       3
5       B1      B1       .       .       2 
;

data want;
 if _n_=1 then do;
   length k $ 80;
   declare hash h();
   h.definekey('k');
   h.definedone();
 end;
set have;
array x{*} $ AI1-AI4;
do i=1 to dim(x);
  if not missing(x{i}) then do; k=x{i};h.ref();end;
end;
want=(h.num_items=1);
h.clear();
drop i k;
run;
CHGMartinez
Fluorite | Level 6
Thanks, it help me a lot!!!
Ksharp,
You alse gave me a solution! thanks!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

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
  • 9 replies
  • 3214 views
  • 7 likes
  • 3 in conversation