how do i reference the last observation with the end statement?
data region_west; set ia.region_west end=last; keep idnum region jcode premium company plan; proc means noprint data=region_west; var premium; output out=mean_premium mean=mean_premium; data tips2; /*add menas when n=1*/ if _N_=1 then set mean_premium; set ia.region_west end=last; proc print; var region premium mean_premium; run; data tips3; set mean_premium; set ia.region_west end=last; proc print; var region premium mean_premium; run;
I tried to put the end statement in my set statement but didn't work.
Are you competing for ugliest and least maintainable code? If yes, you're not quite there, but you deserve an honorable mention. Please look at the many examples of well-formatted code posted by the senior members here.
data tips3;
set mean_premium;
set ia.region_west end=last; /* you define a variable that will be set to 1 when the last observation is read, but you never use it */
proc print; /* the proc statement constitutes a step boundary, so the data step ends here */
var region premium mean_premium;
run;
Since you use two SET statements, it may also be that the last observation of ia.region_west is never read (if mean_premium has less observations).
The END= you used is not a statement in itself, but an option of the SET statement.
@Curious4 wrote:
how do i reference the last observation with the end statement?
data region_west; set ia.region_west end=last; keep idnum region jcode premium company plan; proc means noprint data=region_west; var premium; output out=mean_premium mean=mean_premium; data tips2; /*add menas when n=1*/ if _N_=1 then set mean_premium; set ia.region_west end=last; proc print; var region premium mean_premium; run; data tips3; set mean_premium; set ia.region_west end=last; proc print; var region premium mean_premium; run;I tried to put the end statement in my set statement but didn't work.
data region_west; set ia.region_west end=last; keep idnum region jcode premium company plan; if last then do; *add in what ever you want done on last step;
put "This is the last record!!!!!!!!!!!!" _n_; end; run;
You can conditionally check if you're on the last record and then execute a command as needed. The example above illustrates this, you should be able to check the log and see the statement in the log.
@Curious4 wrote:
how do i reference the last observation with the end statement?
data region_west; set ia.region_west end=last; keep idnum region jcode premium company plan; proc means noprint data=region_west; var premium; output out=mean_premium mean=mean_premium; data tips2; /*add menas when n=1*/ if _N_=1 then set mean_premium; set ia.region_west end=last; proc print; var region premium mean_premium; run; data tips3; set mean_premium; set ia.region_west end=last; proc print; var region premium mean_premium; run;I tried to put the end statement in my set statement but didn't work.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.