BookmarkSubscribeRSS Feed
sling
Fluorite | Level 6

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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 1481 views
  • 0 likes
  • 2 in conversation