first. and last. statements

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

first. and last. statements

Hello all,

data temp;

input bankname brnum cntyname

cards;

Bofa 0 Mecklenburg

Bofa 1 Orange

Bofa 2 Ventura

Bofa 3 Amameda

Welch 0 Contra Costa

Welch 1 Los Angeles

Wlech 3 Tulare

Chase 0 Kern

Chase 1 Shasta

Chase 2 Santa Cruz

chase 3 Yolo

;

run;

data new;

set temp;

retain totbr;

attrib totbr informat=8. format 12.

by bankname;

if first.bankname then do;

totbr = 0;

end;

if last.bankname then output;

run;


I would like to have the brnum '0' information on the dataset than having the last record info.

I want to use first. and last. statements calculate the totbr, otherwise I want to have the first record of bankname(brnum=0) info onthe new dataset.

Would any one help on this?

Thanks in advance.


Accepted Solutions
Solution
‎10-03-2013 05:32 PM
PROC Star
Posts: 7,492

Re: first. and last. statements

Posted in reply to saslovethemost

I'm surprised that no one has responded.  If your data really have mixed case (e.g., chase and Chase), you should pre-process the data to convert bankname to uppercase.

Then, to do what you want, sort by bankname descending brnum, then use the code you indicated.

View solution in original post


All Replies
Solution
‎10-03-2013 05:32 PM
PROC Star
Posts: 7,492

Re: first. and last. statements

Posted in reply to saslovethemost

I'm surprised that no one has responded.  If your data really have mixed case (e.g., chase and Chase), you should pre-process the data to convert bankname to uppercase.

Then, to do what you want, sort by bankname descending brnum, then use the code you indicated.

Respected Advisor
Posts: 3,799

Re: first. and last. statements

GROUPFORMAT?

data temp;
   input bankname $ brnum cntyname &$30.;
  
cards;
Bofa 0 Mecklenburg
bofa 1 Orange
Bofa 2 Ventura
Bofa 3 Amameda
welch 0 Contra Costa
Welch 1 Los Angeles
WelCh 3 Tulare
ChaSe 0 Kern
Chase 1 Shasta
Chase 2 Santa Cruz
chase 3 Yolo
;;;;
   run;


data new;
   set temp;
   format bankname $upcase.;
  
by bankname groupformat notsorted;
  
retain totbr;
   if first.bankname then do;
   totbr =
0;
  
end;
  
if last.bankname then output;
  
run;
proc print;
  
run;
PROC Star
Posts: 7,492

Re: first. and last. statements

Posted in reply to data_null__

DN: I agree with using groupformat (which, I admit, I didn't know about), but I still think that the OP wants to sort by bankname descending brnum

Simply using the groupformat could cause problems if the wrong cases happen to be located somewhere in the middle of that combination.

Occasional Contributor
Posts: 15

Re: first. and last. statements

Posted in reply to data_null__

I think saslovethemost wrote that as an example and forgot the specifics. Anyways, a quick and dirty approach would be:

sort the data first by bankname and then by descending brname and use the same code as you are using currently

proc sort data = temp;

by bankname descending brname ;

run;

and your first. and last. calculation.

Regards,

Somi

Occasional Contributor
Posts: 15

Re: first. and last. statements

Posted in reply to saslovethemost

Ohh I am sorry Arthur already indicated that approach!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 264 views
  • 0 likes
  • 4 in conversation