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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 1 reply
  • 849 views
  • 0 likes
  • 2 in conversation