BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
armoore
Fluorite | Level 6
  1. For the missing values in cholesterol. The code is not working for the output variable chol_status. Each time I run the code the output variable chol_status displays “Safe” instead of a blank space.
  • To identify missing values I thought you used:  if variable-name=. then variable-name=. ;   or               if missing(variable-name) then variable-name=. ;

What would be the correct code for missing values for ?

data results;
set "/home/u44696116/TheCut/input/input44.sas7bdat";
length chol_status $ 15;
drop bp_status weight_status smoking_status;
if cholesterol=. then chol_status=. ;
if cholesterol<200 then chol_status="Safe";
else if cholesterol<=239 then chol_status="High-Borderline";
else if cholesterol>=240 then chol_status="High";
run;



SAS ANSWER

data out;
set "/home/u44696116/TheCut/input/input44.sas7bdat";
length chol_status $ 15;
drop bp_status weight_status smoking_status;
if cholesterol ne . then do;
if cholesterol < 200 then chol_status='Safe';
else if cholesterol <=239 then chol_status='High-Borderline';
else if cholesterol >=240 then chol_status='High';
end;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If the first place you reference the variable is when you are assigning it a numeric value then the variable will defined as numeric.  So define the variable BEFORE using it.

length chol_status $20;

Also you told it to set the missing value to Safe with this statement:

if cholesterol<200 then chol_status="Safe";

SAS will consider any of the 28 missing values that SAS supports as less than any valid value.

 

You probably meant to have another ELSE.

if missing(cholesterol) then chol_status=' ';
else if cholesterol < 200 then chol_status='Safe';
else if cholesterol <=239 then chol_status='High-Borderline';
else if cholesterol >=240 then chol_status='High';

 

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

If the first place you reference the variable is when you are assigning it a numeric value then the variable will defined as numeric.  So define the variable BEFORE using it.

length chol_status $20;

Also you told it to set the missing value to Safe with this statement:

if cholesterol<200 then chol_status="Safe";

SAS will consider any of the 28 missing values that SAS supports as less than any valid value.

 

You probably meant to have another ELSE.

if missing(cholesterol) then chol_status=' ';
else if cholesterol < 200 then chol_status='Safe';
else if cholesterol <=239 then chol_status='High-Borderline';
else if cholesterol >=240 then chol_status='High';

 

armoore
Fluorite | Level 6
Thank you
Jagadishkatam
Amethyst | Level 16

Alternatively you can try the proc format as below, for this you need to create a format and then use that format to derive a new variable.

 

Also, use the libname to call the dataset in the set statement.

 

proc format;
value chol
low-<200='Safe'
200-239='High-Borderline'
240-high='High'
other=' ';
run;

libname dat "/home/u44696116/TheCut/input";

data out;
	length chol_status $ 15;
	set dat.input44;
	drop bp_status weight_status smoking_status;
    chol_status=put(cholesterol,chol.);
run;
Thanks,
Jag

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!

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
  • 3 replies
  • 2111 views
  • 3 likes
  • 3 in conversation