BookmarkSubscribeRSS Feed
sling
Calcite | Level 5

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!

 

1 REPLY 1
ballardw
Super User

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 995 views
  • 0 likes
  • 2 in conversation