Can i get help with this SAS steps I wrote. I am new to SAS and I cannot figure out why I am having errors in the log.
data MSM_IDU;
set testing_data ;
format risk $10.;
if x742 ne '1' then risk='UNKNOWN';
else if g124 in ('1' '3') then do;
if g216a='1' then do;
if g402='1' then risk='MSM/IDU';
else risk='MSM';
end;
RUN;
missing comma in the IN stmt and a missing end
else if g124 in ('1' , '3') then
* full code;
data MSM_IDU;
set testing_data ;
format risk $10.;
if x742 ne '1' then risk='UNKNOWN';
else if g124 in ('1', '3') then do;
if g216a='1' then do;
if g402='1' then risk='MSM/IDU';
else risk='MSM';
end;
end;
RUN;
Hi @adzoyi
It's hard to say without some data to play with.
It seems that an 'end' statement is missing at the end of the program to close the Do loop:
data MSM_IDU;
set testing_data;
format risk $10.;
if x742 ne '1' then risk='UNKNOWN';
else if g124 in ('1' '3') then
do;
if g216a='1' then
do;
if g402='1' then
risk='MSM/IDU';
else
risk='MSM';
end;
end; /* <<-- to be added*/
run;
Tip: to facilitate the identification of syntax error, do not hesitate to format your code and use proper indentation.
Best,
missing comma in the IN stmt and a missing end
else if g124 in ('1' , '3') then
* full code;
data MSM_IDU;
set testing_data ;
format risk $10.;
if x742 ne '1' then risk='UNKNOWN';
else if g124 in ('1', '3') then do;
if g216a='1' then do;
if g402='1' then risk='MSM/IDU';
else risk='MSM';
end;
end;
RUN;
When you get an error best practice for this forum is to copy from the Log the data step or Proc code along with all the messages. The Paste into a code box opened with the </> icon to preserve formatting. The main message windows will reformat text so the diagnostic characters that SAS often supplies do not appear in the correct place.
An example:
190 data junk; 191 set sashelp.class; 192 if sex='F' then do; 193 junk=age*weight; 194 run; 194 run; - 117 ERROR 117-185: There was 1 unclosed DO block. NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.JUNK may be incomplete. When this step was stopped there were 0 observations and 6 variables. WARNING: Data set WORK.JUNK was not replaced because this step was stopped.
The message is moderately clear: there was Do statement without a matching End to close the Do block.
The diagnostic characters appear with the Run statement in this case as it ends the data step and is the first thing SAS can indicate as "not an end statement".
Looks like other community members beat me to the punch.
I'd like to add it's a good idea to format/indent your code which makes it easier to debug
data MSM_IDU;
set testing_data ;
format risk $10.;
if x742 ne '1' then
risk='UNKNOWN';
else
if g124 in ('1', /* <-- NEED THIS COMMA HERE */ '3') then do;
if g216a='1' then do;
if g402='1' then risk='MSM/IDU';
else risk='MSM';
end;
end ; /* <-- NEED THIS EXTRA END */
RUN;
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.