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

Hello there,

I am new user, and got stock in a question which I couldnt get it to run on SAS, any help appreciated.

the question:

  1. Create a dataset called “small” from main and store it in the lab library.  Do this problem in steps: first, use the following “keep” option to reduce the number of variables in this new data set.  Next, sort the data set by child’s gender (csex). Lastly, print a list of the first 10 records for the following variables: mrace frace.

keep dmage mrace dmar dlivord dfage frace csex dbirwt primac wtgain;

 

thank you so much in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

Hi:

  There are a few things wrong with your program. Here's a hint about one of them that should be generating errors. If you are going to reference a dataset in a procedure step you need data= -- so, for example, consider the difference in your PROC SORT versus your PROC PRINT. One is right and the other is not:

Cynthia_sas_0-1600872133792.png

 

In your program, I recommend that you look up the difference between a KEEP statement and a KEEP= option and then correct your syntax accordingly. Also, what is the purpose of the main=csex; assignment statement? Are you trying to create a new variable or column called MAIN by assigning the current value of the variable CSEX? Why is the name of the variable the same as the name of the dataset you are creating? What is this statement really doing and is it doing what you need to do??

Cynthia_sas_1-1600872216409.png

 

Hope this helps,

Cynthia

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Show us what you have tried so far.

--
Paige Miller
Alex901
Calcite | Level 5
this is what i have tried, I know it is not correct so please excuse me.

data main;
set small;
if csex = '1';
keep dmage mrace dmar dlivord dfage frace csex dbirwt primac wtgain;
run;
input dmage mrace dmar dlivord dfage frace csex dbirwt primac wtgain;
proc print data = small (obs 10);
run;
Alex901
Calcite | Level 5

this is what i have tried, I know it is not correct so please excuse me.

data main;
set small;
if csex = '1';
keep dmage mrace dmar dlivord dfage frace csex dbirwt primac wtgain;
run;
input dmage mrace dmar dlivord dfage frace csex dbirwt primac wtgain;
proc print data = small (obs 10);
run;

Tom
Super User Tom
Super User

What purpose do you think that third to last line is doing?
You don't seem to have anything that will sort the data as requested.  How do you propose to sort the data?

Alex901
Calcite | Level 5

okay so I was trying a little and this is what i got

 

title 'Lab 6 – Problem 5';
data main;
set small (keep dmage mrace dmar dlivord dfage frace csex dbirwt primac wtgain);
main = csex;
proc sort main;
by csex;
run;
proc print data = main;
run;

 

thanks

Cynthia_sas
SAS Super FREQ

Hi:

  There are a few things wrong with your program. Here's a hint about one of them that should be generating errors. If you are going to reference a dataset in a procedure step you need data= -- so, for example, consider the difference in your PROC SORT versus your PROC PRINT. One is right and the other is not:

Cynthia_sas_0-1600872133792.png

 

In your program, I recommend that you look up the difference between a KEEP statement and a KEEP= option and then correct your syntax accordingly. Also, what is the purpose of the main=csex; assignment statement? Are you trying to create a new variable or column called MAIN by assigning the current value of the variable CSEX? Why is the name of the variable the same as the name of the dataset you are creating? What is this statement really doing and is it doing what you need to do??

Cynthia_sas_1-1600872216409.png

 

Hope this helps,

Cynthia

Alex901
Calcite | Level 5

THANK YOU SO MUCH!

I ran it now and it worked, sorry I just started to learn SAS and I am still new in this, I really appreciate your help Cynthia!

 

Data small;

Set main;

keep dmage mrace dmar dlivord dfage frace csex dbirwt primac wtgain;

run;

proc sort data = small;

by csex;

run;

proc print data = small (obs = 10);

run;

Cynthia_sas
SAS Super FREQ
Hi:
Right now, the dataset SMALL is just a copy of MAIN, with certain variables kept in the new dataset. So, SMALL should have the same number of rows as MAIN. If you meant to subset MAIN based on some value, it looks like you forgot that statement. But I'm glad you got it working. I highly recommend our free Programming 1 e-learning class which would explain the DATA step, IF statements, KEEP statements, PROC statements and other essential things about SAS syntax and writing programs.

Cynthia

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1101 views
  • 1 like
  • 4 in conversation