Hello,
I need to modify a sas program which is called with an include statement.
Anyway!
Here's a little piece of the code:
Select;
when( vehicle_cd in ('AA', 'PP') type3=1;
...
otherwise type3=2;
end;
When I bring this code into SAS EG, the select , otherwise and end are in red.
I wonder how this code is working properly. Will it be a best practice to modify the code as below?
Data _null_;
Select;
when( vehicle_cd in ('AA', 'PP') type3=1;
...
otherwise type3=2;
end;
run;
For more about the SELECT statement in DATA step, I recommend this blog post from @Rick_SAS .
/* example of using the SELECT statement */
data Heart / view=Heart;
set sashelp.heart;
select (Smoking_Status);
when ('Non-smoker') Smoking_Cat=1;
when ('Light (1-5)') Smoking_Cat=2;
when ('Moderate (6-15)') Smoking_Cat=3;
when ('Heavy (16-25)') Smoking_Cat=4;
when ('Very Heavy (> 25)') Smoking_Cat=5;
otherwise Smoking_Cat=.;
end;
run;
You are talking about what the code looks like in the EDITOR? The editor is not the SAS language, it is just a tool for editing the text that has your code.
What does the SAS log say when you actually send the code to SAS to run?
The editor in PC-SAS does a pretty good job of detecting the syntax with that code. Even without the missing right parenthesis.
But if the DATA statement is not there the SELECT is in red since you cannot use it outside of a data step.
So perhaps the line before the DATA statement is the issue. For example it might be missing a closing semi-colon.
For more about the SELECT statement in DATA step, I recommend this blog post from @Rick_SAS .
/* example of using the SELECT statement */
data Heart / view=Heart;
set sashelp.heart;
select (Smoking_Status);
when ('Non-smoker') Smoking_Cat=1;
when ('Light (1-5)') Smoking_Cat=2;
when ('Moderate (6-15)') Smoking_Cat=3;
when ('Heavy (16-25)') Smoking_Cat=4;
when ('Very Heavy (> 25)') Smoking_Cat=5;
otherwise Smoking_Cat=.;
end;
run;
Hello,
based on your nice example, I have perfom three tests.
One with your code
another one with the use of the select1.sas
select (Smoking_Status);
when ('Non-smoker') Smoking_Cat=1;
when ('Light (1-5)') Smoking_Cat=2;
when ('Moderate (6-15)') Smoking_Cat=3;
when ('Heavy (16-25)') Smoking_Cat=4;
when ('Very Heavy (> 25)') Smoking_Cat=5;
otherwise Smoking_Cat=.;
end;
another with the select2.sas
Data _null_;
select (Smoking_Status);
when ('Non-smoker') Smoking_Cat=1;
when ('Light (1-5)') Smoking_Cat=2;
when ('Moderate (6-15)') Smoking_Cat=3;
when ('Heavy (16-25)') Smoking_Cat=4;
when ('Very Heavy (> 25)') Smoking_Cat=5;
otherwise Smoking_Cat=.;
end;
run;
****
Then I have executed the two programs below . I have noticed no difference.
The only this is with the select2.sas, the fact that I have put my code between data _null_; and run;
make the red codes to desapear
/* example of using the SELECT statement */
data Heart / view=Heart;
set sashelp.heart;
%include "/SAS Snippets/select1.sas";
run;
/* example of using the SELECT statement */
data Heart / view=Heart;
set sashelp.heart;
%include "/SAS Snippets/select2.sas";
run;
Thank you!
When you use %include you're telling SAS to bring in the contents of that file as lines of code -- directly into the location where you used the %include statement. Those lines of code must be syntactically correct and appropriate for running there as if they were embedded directly in the program you're running.
So a select...when clause is valid within a DATA step. Your Select2.sas example includes the DATA step statements, which would in effect embed DATA step statements inside another DATA step...which doesn't make sense and could cause the errors.
Please re-read my first response. The color coding of the editor has nothing to do with whether or not you have created valid code. Ignore it.
There is no way that the SAS program editor can read your mind to know that your intent is in the future to use %INCLUDE to use this snippet of code to build a data step. So without the full data step in the editor window at the same time there is no way that the editor can know that SELECT is being used properly or not.
you are right. I did some test today using data _null_; ... run; and it failed.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.