DATA Step, Macro, Functions and more

finding a specific value in a series of variables

Reply
New Contributor
Posts: 4

finding a specific value in a series of variables

Hi Experts, 

 

I have a dataset like below

DATA survey;
INPUT id r1 r2 r3 r4 r5;
DATALINES;
1 1 0 0 1 1
2 1 0 1 1 1
3 1  . 0 .  1
4 1 1 0 1 1
5 0 1 .  .  1
6 1 0 0 0 0
7 1 0 .  0 1
8 1 1 0 1 1

9 1 0 1 0 1
;

 

I have to generate 2 new variables A=the variable name for the last observation of 0; B = the variable name for the first observation of 1 after the last 0. For example, the 6th row: A=r5, B=''; the 9th row: A=r4, B=r5

 

Is there any idea how I can start? Thank you very much!

 

Super User
Posts: 11,343

Re: finding a specific value in a series of variables

Since this sounds a lot like a "want to learn" I'm gong to provide some instructions not code

Place the varaibles to search into an array

 

Search the array from the largest index to the smallest: Hint: DIM and by -1. This will tell you where you find the 0. Then search from that position forward to find the 1, if any.

 

You can get the name of an array member with the VNAME function.

Ask a Question
Discussion stats
  • 1 reply
  • 141 views
  • 0 likes
  • 2 in conversation