BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Mit
Calcite | Level 5 Mit
Calcite | Level 5

Hi

I would like to replace the blank cells with the last variable value until a new variable value is found.

I have tried using lag function but the number of blanks are not fixed. The number of consecutive blank cells varies.

Attached is the sample data in csv. I would like to get 'data after' from 'data before'.

Any help will me much appreciated.

Thanks

Mit

1 ACCEPTED SOLUTION

Accepted Solutions
shivas
Pyrite | Level 9

Hi,

Try this ...

data samp  ;

   set before ; retain online1 radio1 sem1;

   if online = . then online =online1 ;

   else online1 = online ;

   if radio = . then radio =radio1 ;

   else radio1 = radio ;

   if sem = . then sem =sem1 ;

   else sem1 = sem ;

drop online1 radio1 sem1;

run ;

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

What code did you try?  The normal problem users have with LAG() is to think that it means to take the value from the previous observation.  Instead it functions like a stack and so if you execute it conditionally the behaviour is not at all to retreive the value from the previous observation.

data want ;

   set have;

   lag_online=lag(online);

   lag_radio=lag(radio);

   lag_sem =lag(sem);

   lag_mtv=lag(mtv);

   if online=. then online=lag_online;

   if radio=. then radio=lag_radio;

   if sem=. then sem=lag_sem;

   if mtv=. then mtv=lag_mtv;

   drop lag_: ;

run;

Mit
Calcite | Level 5 Mit
Calcite | Level 5

Hi Tom

Thanks for that. However I used the same code and it repeats the last value once only. There are lag(1) and lag(2) as well. The later will repeat the value twice.

In my data there is not fixed number of times the value would be repeated.

Mit

shivas
Pyrite | Level 9

Hi,

Try this ...

data samp  ;

   set before ; retain online1 radio1 sem1;

   if online = . then online =online1 ;

   else online1 = online ;

   if radio = . then radio =radio1 ;

   else radio1 = radio ;

   if sem = . then sem =sem1 ;

   else sem1 = sem ;

drop online1 radio1 sem1;

run ;

Mit
Calcite | Level 5 Mit
Calcite | Level 5

Hi Shivas

Thanks for that. This has worked.

Mit

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 2753 views
  • 0 likes
  • 3 in conversation