DATA Step, Macro, Functions and more

How To Control Retain Variable Value

Reply
Contributor
Posts: 52

How To Control Retain Variable Value

Hi all,

My data set is Like This,

data chk;

  input x;

  datalines;

12

23

34

11

45

;

Run;

And My desired Output Look Like,

12

12

23

12

23

34

12

23

34

11

12

23

34

11

45

So Pls Guys Help Me out in this problem. Your Valuable Suggestion appreciate.

Thanks In Advance.

Regards,

Dishant Parikh

Valued Guide
Posts: 2,177

Re: How To Control Retain Variable Value

dishant

please

before I offer a solution

why!

is it really just a repeat of the first N obs where N goes from 1 to 5?

Message was edited by: Peter Crawford

Contributor
Posts: 52

Re: How To Control Retain Variable Value

Hi peter,

Actually I am Creating one Macro This Solution Will Helpful me a lot.

can you help me to reach the required output ,it would be grateful.

Valued Guide
Posts: 2,177

Re: How To Control Retain Variable Value

data fun;

do y= 1 to _n_ ;

  set chk point= y ;

  output ;

end ;

if _n_ >= 5 then stop ;

run ;

Contributor
Posts: 52

Re: How To Control Retain Variable Value

Hi Peter,


Thanks A lot For giving the answer and your valuable time.This Solution Will Helpful me a lot.


One More Question we can do with the any other logic or procedure?


Valued Guide
Posts: 2,177

Re: How To Control Retain Variable Value

Why is that not an adequate anserr?

did your One Macro need something else?

Message was edited by: Peter Crawford

Contributor
Posts: 52

Re: How To Control Retain Variable Value

Hi Peter,

yeah obviously its adequate answer and it takes me new level of thinking but as per your logic we need to pass total _n_  in if condition.

If suppose data get updated and few more observation added then again we need to change the if condition right?

So is their any logic in dynamic way.

Thanks In advance.

Valued Guide
Posts: 2,177

Re: How To Control Retain Variable Value

what error messages are you getting?

Respected Advisor
Posts: 3,799

Re: How To Control Retain Variable Value

If you visit the online documentation regarding the SET statement you will find the NOBS option.  Which should suffice for what you call "total _N_".

data chk;
  input x;
  datalines;
12
23
34
11
45
;
Run;

data fun;
   group = _n_;
  
do y= 1 to _n_;
      set chk point=y nobs=nobs;
      output;
     
end;
  
if _n_ >= nobs then stop;
  
run;
proc print;
  
run;
Ask a Question
Discussion stats
  • 8 replies
  • 296 views
  • 0 likes
  • 3 in conversation