BookmarkSubscribeRSS Feed
kariarchie
Calcite | Level 5

Hi y'all! I need to answer this problem:

Create five permanent data sets in one data step base on the categories of variable Mjob. (i.e. one data set contains Mjob=’ At_home’, one contains Mjob=’Health’, …..)

 

This is what I have so far:

 

TITLE 'Problem 2 (5)';

 

DATA 'E:\SAS HW\P_student2' (KEEP=Mjob WHERE Mjob='At_home') 'E:\SAS HW\P_student3' (KEEP=Mjob WHERE Mjob='Health') 'E:\SAS HW\P_student4' (KEEP=Mjob WHERE Mjob='Other') 'E:\SAS HW\P_student5'
(KEEP=Mjob WHERE Mjob='Services') 'E:\SAS HW\P_student6' (KEEP=Mjob WHERE Mjob='Teacher');


SET 'E:\SAS HW\P_student';


RUN;

 

I am not sure how to denote the category of the variable Mjob that I want to show up when I print observations (the WHERE statement does not work). Any help would be appreciated thank you!

3 REPLIES 3
ballardw
Super User

I don't know who is teaching you but generally SAS uses a LIBRARY.DATASETNAME notation so that once a library is created you reference datasets as 

mylib.somename instead of incorporating paths and such making very hard to type and spell names correctly.

 

I have to assume this an assignment that is forcing bad programming practices.

In a data step you need to use an explicit output statement to write records to a specific data set unless you really enjoy writing ugly code.

 

IF you are going to do such it would be a good idea to name the datasets so that  the name means something.

 

I'll show one way to do two data sets. It should be easy to add the rest.

data At_home health;
   set student;
   if mjob='At_home' then output At_home;
   if mjob='Health' then output Health;
run;

Without a library name these will write to the WORK library. This assumes the existing data is in the work library and named student. I am not sure what your actual data set name is because what you show is almost never used.

If you are using data set names like 'E:\SAS HW\P_student' the name must be referenced as 'E:\SAS HW\P_student'n . The N immediately following the quote tells SAS you are using it as a name of something. Context would tell SAS whether it is a data set, such as appearing on a SET statement or part of a "data='some name'n" construct, or a variable.

 

If your "instructor" hasn't covered libraries yet I foresee lots of troubles actually using the instruction. Libraries should be covered in the first hour or so of any reasonably good instruction program. Adding the name of library you made to the names makes them "permanent" data sets.

 

 

andreas_lds
Jade | Level 19

@ballardw : it seems like someone thought, that the whole concept of libraries is to difficult, so it is to skipped in courses and the participants are taught to use the unreadable full-path-syntax instead. Maybe this was initiated by the same person thinking that it is a good idea to teach macro-programming in the first course.

mkeintz
PROC Star

Your program should produce 5 sas data sets, each with an exclusive value of MJOB.  

 

What I don't understand is:

 

I am not sure how to denote the category of the variable Mjob that I want to show up when I print observations (the WHERE statement does not work).

Saying "the WHERE statement does not work" does not work in getting a helpful response.  Show an example, describe what you expected, and what you got.

 

What I think you mean is that you probably got a log message that you didn't report to us.

 

You should change the syntax of your where condition to

(KEEP=Mjob WHERE=(Mjob='Health'))

This is using where as a data set name parameter, which is not the same syntax as a where statement.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1018 views
  • 1 like
  • 4 in conversation