BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

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;

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

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;
SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

View solution in original post

9 REPLIES 9
Tom
Super User Tom
Super User

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?

Reeza
Super User
Your code has syntax errors - missing brackets for starters so it should be red. SAS can correct some of this - and this will be noted in the log.
Your example isn't realistic so I'm not sure what you're asking since it's not valid code.

Tom
Super User Tom
Super User

The editor in PC-SAS does a pretty good job of detecting the syntax with that code. Even without the missing right parenthesis.

image.png

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.

image.png

ChrisHemedinger
Community Manager

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;
SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
alepage
Barite | Level 11

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!

ChrisHemedinger
Community Manager

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.

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
Tom
Super User Tom
Super User

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.

Reeza
Super User
Your SELECT 1 is correct, your SELECT2 is incorrect but IMO I would use a format and not do it this way though it's technically possible.....
alepage
Barite | Level 11

you are right.  I did some test today using data _null_; ... run; and it failed.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2348 views
  • 4 likes
  • 4 in conversation