BookmarkSubscribeRSS Feed
peiy5920
Calcite | Level 5

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.

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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.

 
--
Paige Miller
peiy5920
Calcite | Level 5
Sorry, I am new here. I am not sure how to change titiles?
PaigeMiller
Diamond | Level 26

@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".

 
--
Paige Miller
Patrick
Opal | Level 21

@peiy5920 

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.

unison
Lapis Lazuli | Level 10

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;
-unison
s_lassen
Meteorite | Level 14

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 6 replies
  • 594 views
  • 0 likes
  • 5 in conversation