-3

I need to create a new variable WHLDR given the conditions below. I'm not sure the last else if is correct. So if multi > 1 and ref_1 = 0 if rel =0 and ref_1=1 then the first id which meets this condition whldr=1 if not then whldr =0, and continues. This is my code and sample data below.

data temp_all;
   merge temp_1  (in=inA)
         temp_2  (in=inB)
         temp_3  (in=inC)
   ;
   by id;
   firstid=first.id;

   if multi = 1 then do;
     if rel = 0 then whldr=1;
     else whldr = 0;
   end;

   else if multi > 1 and ref_1 >= 1 then do;
     if rel =0 and ref_1=1 then whldr=1;
     else whldr = 0;
   end;

   else if multi > 1 and ref_1 = 0 then do;
     if rel =0 and ref_1=1 then do;
       if rel =0 and ref_0 ne '0' then do;
         if first.id=1 then  whldr=1 ;
         else whldr=0;
       end;
     end;
   end;
run;

Here is sample data:

data have ;
  input id a rel b multi ;
cards;
105 . 0 0 1
110 1 0 1 1
110 0 1 1 1
110 . 2 1 1
113 1 0 1 1
113 2 1 1 1
113 0 2 1 1
113 0 2 1 1
135 1 0 1 1
135 0 1 1 1
176 1 0 1 1
176 0 1 1 1
189 1 0 1 1
189 2 1 1 1
189 0 4 1 1
189 0 4 1 1
;
Tom
  • 47,574
  • 2
  • 16
  • 29
user601828
  • 499
  • 3
  • 7
  • 17
  • 1
    You need to explain what you want in more detail. For example how are you defining a person? Do you want the first person in the whole dataset? or within some other grouping variable like state? Posting example input and result data would help clarify what you want. – Tom Nov 24 '16 at 16:50
  • Thats my code above. The persons is whldr, and I want the first person in the whole data set that meets the last condition. I have a person level data, and I'm trying to get the first person (B) that meets that condition, and the result will be a household level dataset. – user601828 Nov 24 '16 at 17:10
  • I cannot evaluate your complex if/then structure to see if it is correct without more information about what you wanted. But if it is flagging the observation you want then perhaps you just need to add an `output;` and `stop;` statement so that you get a single observation result dataset. – Tom Nov 24 '16 at 17:25
  • how can i paste a table so you can see the data? I'm creating a new variable 'whldr' on the conditions specified above. – user601828 Nov 24 '16 at 17:32
  • That code isn't complete. It helps if you post the full code that illustrates your problem, and ideally data so we can replicate the issue. Otherwise its a guessing problem. – Reeza Nov 24 '16 at 22:14
  • I pasted in the sample data you tried to enter else where. Can you edit the question to indicate which record is the FIRST based on the logic you want to apply? – Tom Nov 24 '16 at 23:18
  • You need to give us some sample data that actually matches your code. And an explanation in words for what the rules are for creating the new variable WHLDR. Make your sample data have cases that exercise each of the pathways in your logic. Include in your sample data what you expect the value of the new variable WHLDR to be for each observation. – Tom Nov 25 '16 at 16:16
  • @user601828, please stop editing other questions/answers to contain yours - these edits **will be rejected** and could be considered vandalism. – Aurora0001 Nov 25 '16 at 16:53

1 Answers1

1

If you have a variable named WHLDR and you want the first observation where it has the value 1 then you can run a data step like this.

data want ;
  set have (obs=1);
  where whldr=1 ;
run;
Tom
  • 47,574
  • 2
  • 16
  • 29
  • I'm creating the variable WHLDR based on the conditions in the if then else statement. So WHLDR gets a value of 1 or 0 based on the three conditions. – user601828 Nov 24 '16 at 18:02
  • The first record is the first id. So in the input data set above, the id 103 has three records, so the first record would get a whldr=1 if it meets the conditions in the last else if block of the code above. – user601828 Nov 25 '16 at 17:56
  • Did you mean 113? Note that the way you have the nested IF statement none of the sample records can get to the line of code that checks the FIRST.ID flag since they all have MULTI=1. – Tom Nov 25 '16 at 18:26
  • Sorry, yes, I meant 113. – user601828 Nov 25 '16 at 20:02
  • So your saying to move the first.id=1 to the top of the last block of else if? – user601828 Nov 25 '16 at 20:04
  • You just need to write logic that will work. We can't help you if you cannot explain what you want in either words or examples. – Tom Nov 26 '16 at 04:04
  • Sorry, I was in a rush, The sample dataset I posted just happen to have MULTI=1, but in the full dataset the variable MULTI, ranges from 1 to 8. – user601828 Nov 26 '16 at 06:28
  • This is what I need to code and I cant seem to get it to cover all the conditions. a. Merge TEMP_MRG2 and TEMP_REFPER1 and TEMP_MULTIHHLDR b. By cmid to create person level file c. If MULTIHHLDR=1 then do; if rel=0 then whohhldr=1; else whohhldr=0; end; d. ELSE if MULTIHHLDR>1 and REFPER1>=1 then do; if rel=0 and refper=1 then whohhldr=1; else whohhldr=0; end; e. ELSE if MULTIHHLDR>1 and REFPER1=0 then whohhldr=1 for the first person with rel=0 and refper not equal 0 (NOTE: if refper=0 for all household members then set whohhldr=1 for first person with rel=0) f. Else whohhldr=0 – user601828 Dec 02 '16 at 22:14
  • Forget trying to explain it with code. Explain the rules using words and examples. – Tom Dec 03 '16 at 00:29