BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Taliah
Obsidian | Level 7

Hello, I have a data set like:

A  B

1   11

2  12

3  13

4  0

5  0

6  0

7  0

The goal is to have SAS search for all locations of b=0 and enter instead the lag of b, draging it throughout all "0"s, to get

1  11

2  12

3  13

4  13

5  13

6  13

7  13

I'm using the code

data test1;

set test;

x=lag(b);

if b=0 then b=x;

run;

Only b in line 4 gets to be 13, not the following rows, in them b stays 0.

Any help will be appriciated. This data set is an example, we do not know how many row there are with b>0 and how many with b=0, those rows can be at the end but also in other placed in the data set. Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

It's simpler to create a new variable:

 

data want;
   set have;
   retain C;
   if b ne 0 then c=b;
   drop b;
   rename c=b;
run;

 C contains the proper values.  So the DROP statement gets rid of the original variable B.  And the RENAME statement changes the variable name from C (with proper values) to B (which is no longer being used).

View solution in original post

2 REPLIES 2
Astounding
PROC Star

It's simpler to create a new variable:

 

data want;
   set have;
   retain C;
   if b ne 0 then c=b;
   drop b;
   rename c=b;
run;

 C contains the proper values.  So the DROP statement gets rid of the original variable B.  And the RENAME statement changes the variable name from C (with proper values) to B (which is no longer being used).

Taliah
Obsidian | Level 7

works great, thank you!

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