08-26-2015 02:16 PM
I have the following code:
DATA WORK.PASS1 (DROP= G M);
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";
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.
08-26-2015 03:12 PM
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'.
08-26-2015 03:47 PM
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:
infile cards dsd;
input a$ g$16.;
if g = 'Total Calls' then delete;
if scan(a,1) = 'MM' and _N_ = 10 then action = 'Arizona Calls';
08-27-2015 02:51 AM
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.
IF SCAN(A,1)="MM" AND _N_=95 THEN do;
08-27-2015 04:55 AM
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);
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?? */