BookmarkSubscribeRSS Feed
shubhamtiwari
Fluorite | Level 6

The program uses sashelp.heart dataset. 

 

 

This program is intended to:

- Divide the observations of sashelp.heart into three data sets, work.highchol, work.lowchol, and work.misschol
- Only observations with cholesterol below 200 should be in the work.lowchol data set.
- Only Observations with cholesterol that is 200 and above should be in the work.highchol data set.
- Observations with missing cholesterol values should only be in the work.misschol data set.

 

 

How many observations are in the work.lowchol data set?

 

 

 

 

please help

 

 

12 REPLIES 12
Kurt_Bremser
Super User

Write your code and run it. 

 

The communities are meant to help you in doing your work, they're not a place to have your homework done for free.

 

If your code leaves you puzzled, post it and the log from it, so we can help you in diagnosing

shubhamtiwari
Fluorite | Level 6

data work.lowchol work.highchol work.misschol;
set sashelp.heart;
if cholesterol lt 200 then output work.lowchol;
else if cholesterol ge 200 then output work.highchol;
else if cholesterol '.' then output work.misschol;
run;


proc print;
run;

 

 

 

 

LOG

 

 

 

205 data work.lowchol work.highchol work.misschol;
206 set sashelp.heart;
207 if cholesterol lt 200 then output work.lowchol;
208 else if cholesterol ge 200 then output work.highchol;
209 else if cholesterol '.' then output work.misschol;
---
388
200
ERROR 388-185: Expecting an arithmetic operator.

ERROR 200-322: The symbol is not recognized and will be ignored.

210 run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.LOWCHOL may be incomplete. When this step was stopped there were 0
observations and 17 variables.
WARNING: Data set WORK.LOWCHOL was not replaced because this step was stopped.
WARNING: The data set WORK.HIGHCHOL may be incomplete. When this step was stopped there were 0
observations and 17 variables.
WARNING: Data set WORK.HIGHCHOL was not replaced because this step was stopped.
WARNING: The data set WORK.MISSCHOL may be incomplete. When this step was stopped there were 0
observations and 17 variables.
WARNING: Data set WORK.MISSCHOL was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds


211
212
213 proc print;
214 run;

NOTE: There were 152 observations read from the data set WORK.MISSCHOL.

 

 

@Kurt_Bremser  I have no intention for this community to do my work for free , I didn't know that i have to to share my code and log.

 

If you could help me with the above problem then thank you otherwise i will try to find the help by some other mean.

 

regards

Jagadishkatam
Amethyst | Level 16

Please try the below code

 

data lowchol highchol misschol;
set sashelp.heart;
if cholesterol ne . and cholesterol lt 200 then output lowchol;
if cholesterol ge 200 then output highchol;
if cholesterol eq . then output misschol;
run;
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

alternatively with else if

 

data lowchol highchol misschol;
set sashelp.heart;
if cholesterol ne . and cholesterol lt 200 then output lowchol;
else if cholesterol ge 200 then output highchol;
else if cholesterol eq . then output misschol;
run;
Thanks,
Jag
Naveen_369
Calcite | Level 5

Thank you Sir. It was very helpful.  I wasted nearly a day solving the missing chol. numeric value. 

Amir
PROC Star

Based on a modification to @Jagadishkatam 's 2nd response, the following untested code is worth trying.

 

You'll notice that the value of cholesterol is only tested twice, rather than four times, this was achieved by checking for missing first, as it is the numerical equivalent of minus infinity, which is why Jag's suggested solutions ensure cholesterol is not missing when testing if it is lt 200.

 

Also the missing() function is being used to test for a missing value as the representation of missing can be changed from the default of '.' to another character by setting the option missing, e.g., options missing = '?';.

 

data lowchol highchol misschol;
   set sashelp.heart;

   if missing(cholesterol) then
      output misschol;
   else
      if cholesterol lt 200 then
         output lowchol;
      else
         output highchol;
run;

 

Regards,

Amir.

tomrvincent
Rhodochrosite | Level 12
Why post untested code?
Naveen_369
Calcite | Level 5

Thank you for the solution. It's been helpful in solving the missing numerics.... Cholesterol variable

Kurt_Bremser
Super User
207 if cholesterol lt 200 then output work.lowchol;

Here you use the comparison operator "lt" (less than), which works.

208 else if cholesterol ge 200 then output work.highchol;

Here you also use a comparison operator, and it also works.

209 else if cholesterol '.' then output work.misschol;

But here you have no comparison operator, which crashes. Why did you think you don't need the operator here?

Also note that you try to compare a numeric variable with a character value, this forces SAS to convert the values. Do not rely on SAS to do such conversions in the way you expect; instead be specific and use the correct representation for a missing numeric value, which is the dot without quotes.

 

Personally, I like @Amir's suggestion most.

Patrick
Opal | Level 21

@shubhamtiwari 

One of the important things and why you need to test for missings first: SAS treats numerical missings as minus infinite and though a selection like: if some_variable < 200 then ... will always include the missings in the selection.

I like @Amir's proposition the most (only the code indention would need some fixing).

ballardw
Super User

While there are reasons to split data at times such things as counting is not one of them.

One approach to counting groups of records is to use a Format to create different text for a given measurement. Most analysis, graphing and reporting procedures in SAS will use the groups created by a format.

This is a very flexible tool for analysis as you can change the groups by assigning a different format and not creating data sets or even new variables.

 

Proc format library=work;
value chol
. = 'Missing'
low -<200 = 'Below 200'
200 - high= '200 or more';
value cholalt
. = 'Missing'
low -<100 = 'Below 100'
100 -<200 = '100 to < 200'
200 - high= '200 or more';

run;

proc freq data=sashelp.heart;
   title '3 level cholesterol';
   tables cholesterol/missing;
   format cholesterol chol.;
run;title;
proc freq data=sashelp.heart;
   title '4 level cholesterol';
   tables cholesterol/missing;
   format cholesterol cholalt.;
run; title;

Note that the code above is posted in a code box opened using the forum's {I} icon. This preserved my code format and in with logs will maintain format especially for things like error diagnostics.

tomrvincent
Rhodochrosite | Level 12
data work.lowchol work.highchol work.misschol;
set sashelp.heart;
if cholesterol eq . then output work.misschol;
else if cholesterol lt 200 then output work.lowchol;
else if cholesterol ge 200 then output work.highchol;
run;

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!

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.

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
  • 12 replies
  • 3413 views
  • 14 likes
  • 8 in conversation