Help using Base SAS procedures

IF _N_=95 THEN

Reply
Contributor
Posts: 28

IF _N_=95 THEN

Hi,

I have the following code:

DATA WORK.PASS1 (DROP= G M);

SET WORK.TELEMKT;

RETAIN MONTH ACTION;

IF INDEXW(A,"Summary") THEN MONTH=UPCASE(SUBSTR(A,1,3));

IF G="Total Calls" THEN DELETE;

IF G="Total Appointments" THEN DELETE;

IF G="Total Contacts" THEN DELETE;

IF A=" " AND E=" " THEN DELETE;

IF SCAN(A,1)="MM" AND _N_=95 THEN ACTION="Arizona Calls";

RUN;

This part of the code: IF SCAN(A,1)="MM" AND _N_=95 THEN DELETE; does not work. The new variable is created, but not the new observation.

Valued Guide
Posts: 860

Re: IF _N_ counter

1)  You don't have 'THEN DELETE' in the code, only in your question does it say delete.

2) You should pull up the first 100 obs, look at what line 95 has, if it doesn't match your if statement you won't get 'Arizona Calls'.

Contributor
Posts: 28

Re: IF _N_ counter

Posted in reply to Steelers_In_DC

i corrected it...ido not want to delete. I want to create a new observation under a new variable. It still did not work.

Valued Guide
Posts: 860

Re: IF _N_ counter

Did you pull up the first 100 obs and look at the 95th line?  Your code isn't the problem it's the incoming data:

data have;

infile cards dsd;

input a$ g$16.;

cards;

MM,Total Calls

MM,Total Calls

MM,Total Calls

MM,Total Calls

MM,Total Calls

MM,Total Calls

MM,Total Calls

MM,Total Calls

MM,Total Calls

MM,

;

DATA WANT;

set have;

if g = 'Total Calls' then delete;

if scan(a,1) = 'MM' and _N_ = 10 then action = 'Arizona Calls';

run;

it works

Super Contributor
Posts: 345

Re: IF _N_ counter

gobejo wrote:

i corrected it...ido not want to delete. I want to create a new observation under a new variable. It still did not work.

I recommend reading Step-by-Step Programming with Base SAS(R) Software. If you want an additional observation, you have to add an "output" statement.

data work.pass1;

...

output;

IF SCAN(A,1)="MM" AND _N_=95 THEN do;

     ACTION="Arizona Calls";

     output;

end;

run;

Super User
Super User
Posts: 7,970

Re: IF _N_=95 THEN

Hi,

Some tips (without knowing the data):

Please don't write code all in upper case, and use indentation.  Also, you can use in () rather than the 3 if statements, makes it a bit clearner.  Finally, I wouldn't recommend using position of record in the dataset as your basis for logic.  Quite a lot of things can change that, for instance, if you sort the dataset, or add records.  Its a good way to introduce bugs into your code.  Use data based logic.

data work.pass1 (drop= g m);

  set work.telemkt;

  retain month action;

  if indexw(a,"summary") then month=upcase(substr(a,1,3));

  if g in ("total calls","total appointments","total contacts") then delete;

  if a=" " and e=" " then delete;

  if scan(a,1)="mm" and _n_=95 then action="arizona calls";   /* Why use a postition of the row in the dataset for your logic?? */

run;

Ask a Question
Discussion stats
  • 5 replies
  • 418 views
  • 0 likes
  • 4 in conversation