BookmarkSubscribeRSS Feed
gobejo
Calcite | Level 5

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.

5 REPLIES 5
Steelers_In_DC
Barite | Level 11

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'.

gobejo
Calcite | Level 5

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

Steelers_In_DC
Barite | Level 11

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

andreas_lds
Jade | Level 19

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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