Reputation: 203
I have this dataset
DATA Problem3;
INPUT name $ Smoke_Tobacco $ Drink_Alcohol $ Take_Illegal_Drugs $ Drink_Soda $;
DATALINES;
Tom yes no no yes
Harry Yes Yes Yes No
Jim No No No Yes
Bob Yes No No Yes
Andy No Yes No Yes
Cody yes no no no
Ed Yes no no yes
Greg no Yes no No
Dave Yes No Yes no
;
RUN;
And I want to use loops to change all "yes" responses to "Yes" and all "no" responses to "No".
My idea is to use arrays for this such as what is shown in the Little SAS workbook
DATA songs;
INFILE 'c:\MyRawData\KBRK.dat';
INPUT City $ 1-15 Age wj kt tr filp ttr;
ARRAY song (5) wj kt tr filp ttr;
DO i = 1 TO 5;
IF song(i) = 9 THEN song(i) = .;
END;
Run;
Which replaces "9" with ".". So I edit my code to
DATA Problem3;
INPUT name $ Smoke_Tobacco $ Drink_Alcohol $ Take_Illegal_Drugs $ Drink_Soda $;
DATALINES;
Tom yes no no yes
Harry Yes Yes Yes No
Jim No No No Yes
Bob Yes No No Yes
Andy No Yes No Yes
Cody yes no no no
Ed Yes no no yes
Greg no Yes no No
Dave Yes No Yes no
;
ARRAY Answer (4) Smoke_Tobacco Drink_Alcohol Take_Illegal_Drugs Drink_Soda;
DO i=1 TO 9;
IF Answer(i) = 'yes' THEN Answer(i)= 'Yes';
ELSE IF Answer(i) = 'no' THEN Answer(i)= 'No';
END;
RUN;
But I get errors saying that the lines in my addition are either not valid or out of order. How do I go about fixing this?
Upvotes: 1
Views: 83
Reputation: 51621
You cannot add statements after the end of the data step!
Just move your new statement to before the DATALINES;
statement that marks the end of your data step and the beginning of your in-line data. Also make sure the upper bound on your do
loop matches the size of your array. Let SAS figure that out for you. do i=1 TO dim(answer);
This is the second confusion along these lines that I have seen recently? I wonder if it is caused by people reflexively adding an extra run;
statement after the end of their data steps that use in-line data? That extra run;
is not needed and becomes a new empty step, not part of the original data step.
Upvotes: 0
Reputation: 21294
You have to put the array statements BEFORE the datalines. Nothing after the data is processed.
DATA Problem3;
INPUT name $ Smoke_Tobacco $ Drink_Alcohol $ Take_Illegal_Drugs $ Drink_Soda $;
ARRAY Answer (4) $3. Smoke_Tobacco Drink_Alcohol Take_Illegal_Drugs Drink_Soda;
DO i=1 TO 4;
answer(i)=propcase(answer(i));
END;
DATALINES;
Tom yes no no yes
Harry Yes Yes Yes No
Jim No No No Yes
Bob Yes No No Yes
Andy No Yes No Yes
Cody yes no no no
Ed Yes no no yes
Greg no Yes no No
Dave Yes No Yes no
;
RUN;
Upvotes: 1