Help using Base SAS procedures

Retain Statement

Reply
Contributor
Posts: 28

Retain Statement

Below is an example of what i am trying to achieve.

DATA HAVE;
INFILE DATALINES DSD;
INPUT A $CHAR50.;
DATALINES;
June Summary

Responses
MM Regional Calls
ARIZONA
APPOINTMENTS
Contacts
WM AZ
APPOINTMENTS
Contacts
San Diego
APPOINTMENTS
Contacts
LADT
APPOINTMENTS
Contacts
San Gabriel Valley
APPOINTMENTS
Contacts
Orange County+Ontario
APPOINTMENTS
Contacts
Long Beach
APPOINTMENTS
Contacts
SBB
Arizona
APPOINTMENTS
Contacts
San Diego
APPOINTMENTS
Contacts
Los Angeles
APPOINTMENTS
Contacts
Long Beach
APPOINTMENTS
Contacts
San Jose
APPOINTMENTS
Contacts
;
RUN;

DATA WANT;
INFILE DATALINES DSD DLM="|" TRUNCOVER;
INPUT A :$CHAR30. B :$CHAR3. C :$CHAR30.;
DATALINES;
ARIZONA|MM|ARIZONA
APPOINTMENTS|MM|ARIZONA
Contacts|MM|ARIZONA
WM AZ|MM|WM AZ
APPOINTMENTS|MM|WM AZ
Contacts|MM|WM AZ
San Diego|MM|San Diego
APPOINTMENTS|MM|San Diego
Contacts|MM|San Diego
LADT|MM|LADT
APPOINTMENTS|MM|LADT
Contacts|MM|LADT
San Gabriel Valley|MM|San Gabriel Valley
APPOINTMENTS|MM|San Gabriel Valley
Contacts|MM|San Gabriel Valley
Orange County+Ontario|MM|Orange County+Ontario
APPOINTMENTS|MM|Orange County+Ontario
Contacts|MM|Orange County+Ontario
Long Beach|MM|Long Beach
APPOINTMENTS|MM|Long Beach
Contacts|MM|Long Beach
Arizona|SBB|Arizona
APPOINTMENTS|SBB|Arizona
Contacts|SBB|Arizona
San Diego|SBB|San Diego
APPOINTMENTS|SBB|San Diego
Contacts|SBB|San Diego
Los Angeles|SBB|Los Angeles
APPOINTMENTS|SBB|Los Angeles
Contacts|SBB|Los Angeles
Long Beach|SBB|Long Beach
APPOINTMENTS|SBB|Long Beach
Contacts|SBB|Long Beach
;
RUN;

Valued Guide
Posts: 860

Re: Retain Statement

Pretty crude and specific but here is a solution.  Won't work for any broader examples.

data want2(keep=a b c);

set have;

count + 1;

retain c;

b = 'MM';

if a = 'MM Regional Calls' then count = 0;

if count = 4 then count = 1;

if count = 1 then c = a;

if 4<=_N_<=48;

run;

Go Pirates

Contributor
Posts: 28

Re: Retain Statement

Posted in reply to Steelers_In_DC

Thank you for the isea Mark. But, i was wondering why i do not see SBB in DATA Want

Super User
Posts: 11,343

Re: Retain Statement

Probably because the "rule" for why SBB takes the place of MM in the data is not in any way obvious. Do you have other codes in that role as well? You would have to test each record to see if it is the appropriate code and assign to the retained variable.

Valued Guide
Posts: 860

Re: Retain Statement

No idea, it's in my output.  You 'd have to provide your code for any additional help.  A little note, you didn't mention that anywhere in your initial post. 

Ask a Question
Discussion stats
  • 4 replies
  • 326 views
  • 0 likes
  • 3 in conversation