BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8
DATA &Portfolio_VAL2(Keep=MOB_VAL_P MOB );
set &Portofo._VAL;
by MOB;
if first.MOB then CNT=0;
CNT+1;
MOB_VAL_P=cnt/&total_val,;
if last.MOB;
run;

hi can you let me know what does the statement "by MOB;" doing on the third row? what does it mean when it comes after set statement?  also should the "if last.MOB" statement put higher up? or it does not matter where it is positioned? 

3 REPLIES 3
LinusH
Tourmaline | Level 20

It initiates BY group processing, a prt of it is creation the autmatic.first and .last variables.

It's a whole concept, you could read about in the documentqation.

I think you need to keep .last where it is, since it stops processing of subsequent statements for that record (if it doesn't meet the criteria).

The easiest way to find out how things work, try to run the code yourself, examine the results, and if needed use the data step debugger.

Data never sleeps
PaigeMiller
Diamond | Level 26
if last.mob;

probably belongs where it is.

 

You can easily see what effect it has, by deleting it, or moving it, and looking at the data set, and see what happens. As stated by @LinusH you should really read the documentation on these extremely important FIRST. and LAST. variables.

--
Paige Miller
Kurt_Bremser
Super User

You can move the subsetting IF up one line, to avoid calculating MOB_VAL_P in ever data step iteration, when it is really needed only once. I also streamlined the first condition, so that the unnecessary increment in the first observation of a group is avoided.

data &Portfolio_VAL2 (keep=MOB_VAL_P MOB );
set &Portofo._VAL;
by MOB;
if first.MOB
then CNT = 1;
else CNT + 1;
if last.MOB;
MOB_VAL_P = cnt / &total_val.;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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