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

Hello, I am looking for some advice since I have had problems creating two data sets composed of females and males from a permanent data set in SAShelp.

I used the if statement, but the data set that was created from there was empty.

Could you help me see what the problem is?

Thanks in advance

I added the code and log data.

libname assignew " /folders/myfolders/study4";
data assignew;
set SASHELP.HEART;
run;
proc print data=assignew (obs=10);
run;
proc contents data=assignew varnum;
run;
data fem;
set  SASHELP.HEART;
if sex = 'f' then  output Fem;
run;
data male;
set  SASHELP.HEART;
if sex = 'm' then  output male;
run;
proc freq data=FEM ;
tables Smoking_Status;
run;

LOG

 
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 libname assignew " /folders/myfolders/study4";
NOTE: Libref ASSIGNEW was successfully assigned as follows:
Engine: V9
Physical Name: /folders/myfolders/study4
74 data assignew;
75 set SASHELP.HEART;
76 run;
 
NOTE: There were 5209 observations read from the data set SASHELP.HEART.
NOTE: The data set WORK.ASSIGNEW has 5209 observations and 17 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
 
77 proc print data=assignew (obs=10);
78 run;
 
NOTE: There were 10 observations read from the data set WORK.ASSIGNEW.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.06 seconds
cpu time 0.06 seconds
 
 
79 proc contents data=assignew varnum;
80 run;
 
NOTE: PROCEDURE CONTENTS used (Total process time):
real time 0.05 seconds
cpu time 0.05 seconds
 
 
81 data fem;
82 set SASHELP.HEART;
83 if sex = 'f' then output Fem;
84 run;
 
NOTE: There were 5209 observations read from the data set SASHELP.HEART.
NOTE: The data set WORK.FEM has 0 observations and 17 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
 
 
85 data male;
86 set SASHELP.HEART;
87 if sex = 'm' then output male;
88 run;
 
NOTE: There were 5209 observations read from the data set SASHELP.HEART.
NOTE: The data set WORK.MALE has 0 observations and 17 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
 
89 proc freq data=FEM ;
90 tables Smoking_Status;
91 run;
 
NOTE: No observations in data set WORK.FEM.
NOTE: PROCEDURE FREQ used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
 
92
93 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
105

 

1 ACCEPTED SOLUTION

Accepted Solutions
BrianGaines
SAS Employee

Hi @xoxozav_1,

 

If you look at the SASHELP.HEART data set, you see that the  Sex variable is coded as Female and Male, not m and f.  So if you change your code to 

 

data fem;
   set SASHELP.HEART;
   if sex = 'Female' then output Fem;
run;

then it should create a fem data set that includes only the Female observations from SASHELP.HEART.

 

You might also consider using a WHERE clause instead:

data fem2;
   set SASHELP.HEART(where=(sex = 'Female'));
run;

If you look at the log for both approaches, you see that the first approach reads in all 5209 observations from SASHELP.HEART, whereas the second approach with the WHERE clause reads in only the 2873 Female observations of interest.  This can help with efficiency.  

 

Does that solve your problem?

 

Thanks,

 

-Brian

 

View solution in original post

1 REPLY 1
BrianGaines
SAS Employee

Hi @xoxozav_1,

 

If you look at the SASHELP.HEART data set, you see that the  Sex variable is coded as Female and Male, not m and f.  So if you change your code to 

 

data fem;
   set SASHELP.HEART;
   if sex = 'Female' then output Fem;
run;

then it should create a fem data set that includes only the Female observations from SASHELP.HEART.

 

You might also consider using a WHERE clause instead:

data fem2;
   set SASHELP.HEART(where=(sex = 'Female'));
run;

If you look at the log for both approaches, you see that the first approach reads in all 5209 observations from SASHELP.HEART, whereas the second approach with the WHERE clause reads in only the 2873 Female observations of interest.  This can help with efficiency.  

 

Does that solve your problem?

 

Thanks,

 

-Brian

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 1 reply
  • 1477 views
  • 0 likes
  • 2 in conversation