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

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
Quartz | Level 8

works great, thank you!

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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