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;
Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!

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;
Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!
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.

Check out SAS Innovate on-demand content! Watch the main stage sessions, keynotes, and over 20 technical breakout sessions!
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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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