BookmarkSubscribeRSS Feed
dishant
Calcite | Level 5

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

8 REPLIES 8
Peter_C
Rhodochrosite | Level 12

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

dishant
Calcite | Level 5

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.

Peter_C
Rhodochrosite | Level 12

data fun;

do y= 1 to _n_ ;

  set chk point= y ;

  output ;

end ;

if _n_ >= 5 then stop ;

run ;

dishant
Calcite | Level 5

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?


Peter_C
Rhodochrosite | Level 12

Why is that not an adequate anserr?

did your One Macro need something else?

Message was edited by: Peter Crawford

dishant
Calcite | Level 5

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.

Peter_C
Rhodochrosite | Level 12

what error messages are you getting?

data_null__
Jade | Level 19

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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