Hi all,
My sas code like this:
if EGFR_CKD_EPI<30 then EGFR_CKD_EPI=<30;
if EGFR_CKD_EPI>=30 and EGFR_CKD_EPI<45 then EGFR_CKD_EPI=30 - 45;
if EGFR_CKD_EPI>=45 and EGFR_CKD_EPI<60 then EGFR_CKD_EPI=45 - 60;
if EGFR_CKD_EPI>=60 then EGFR_CKD_EPI=>60;
Are there some problems with the code? Thanks.
What happens when you run it?
Please go back and provide a meaningful subject line for your question. "A simple question" as a title means nothing and using meaningful titles help all of us.
@peiy5920 wrote:
Sorry, I am new here. I am not sure how to change titiles?
You click on the "gear icon" at the top right of your original message and then select "Edit Reply".
Is that a test question?
1. EGFR_CKD_EPI appears to be numeric so you can't assign a non-numeric value like <30 to it
2. you would need to pass in a string in quotes like '<30' - but that's not possible here (see 1.)
Assuming that's for printing numerical values you normally would define and use a Format for this.
EGFR_CKD_EPI is numeric. It looks like you are trying to create some sort of grouping based on the value of EGFR_CKD_EPI.
A few options include creating a character variable called group and writing the logic for it in the data step (shown in want_1 step below) or using proc format to pull the logic out of the data step and then create group as shown in want_2 below.
data have;
do EGFR_CKD_EPI=1 to 100;
output;
end;
run;
*Format embedded in data step;
data want_1;
set have;
format group $8.;
if EGFR_CKD_EPI<30 then
group='<30';
if EGFR_CKD_EPI>=30 and EGFR_CKD_EPI<45 then
group='30 -< 45';
if EGFR_CKD_EPI>=45 and EGFR_CKD_EPI<60 then
group='45 -< 60';
if EGFR_CKD_EPI>=60 then
group='>=60';
run;
*Create format using proc format;
proc format;
value bin low-<30="<30" 30-<45="30 -< 45" 45-<60="45 -< 60" 60-high=">=60";
run;
*Create group based on custom format;
data want_2;
set have;
group=put(EGFR_CKD_EPI, bin.);
run;
*Checking frequencies of groups;
proc freq data=want_1 order=data;
tables group/nocol nocum norow nopercent;
run;
proc freq data=want_2 order=data;
tables group/nocol nocum norow nopercent;
run;
I would go with the second solution (the format) suggested by @unison.
In a lot of cases you would not have to create a new variable using the format, just apply it to the variable, e.g. the PROC FREQ example:
proc freq data=have order=data;
tables EGFR_CKD_EPI/nocol nocum norow nopercent;
format EGFR_CKD_EPI bin.;
run;
But if you need to do it in code, use some ELSEs in your code:
if EGFR_CKD_EPI<30 then group='<30 ';
else if EGFR_CKD_EPI<45 then group='30 - 45';
else if EGFR_CKD_EPI<60 then group='45 - 60';
else group='>60';
Or use a select statement, that does the same thing:
select;
when(EGFR_CKD_EPI<30) group='<30 ';
when(EGFR_CKD_EPI<45) group='30 - 45';
when(EGFR_CKD_EPI<60) group='45 - 60';
otherwise group='>60';
end;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.
Ready to level-up your skills? Choose your own adventure.