Help using Base SAS procedures

How to use IF statement with quantile

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

How to use IF statement with quantile

I have several GPA sample. I want to make new variable GPA_Q which has BAD(less 25%), AVG(25% - 75%), GOOD(more 75%).

data work.data;

set work.data;

    IF (????) THEN GPA_Q = 'Bad' ;

    ELSE IF (????) THEN GPA_Q= 'Average' ;

    ELSE GPA_Q = 'Good' ;

run;

something good idea?

Thanks in advance.


Accepted Solutions
Solution
‎10-30-2012 05:20 PM
Super User
Posts: 19,770

Re: How to use IF statement with quantile

Merging in the two variables is easier unless you need to use macro variables.

proc means data=sashelp.class noprint;

    var weight;

    output out=want(keep=qSmiley Happy q1=q1 q3=q3;

run;

data class;

    set sashelp.class ;

    if _n_=1 then set want;

    length cat $8.;

    if  0 <weight < q1 then cat='Bad';

    else if weight<=q3 then cat='Average';

    else cat='Good';

run;

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: How to use IF statement with quantile

a sample code:

/* get Q25 and Q75 */

proc means data=sashelp.class noprint;

var weight;

output out=want(keep=qSmiley Happy q1=q1 q3=q3;

run;

/* create macro variables */

data _null_;

  set want;

  call symputx('q25',q1);

  call symputx('q75',g3);

  run;

/*********************/

  data final;

   length gpa $8;

   set sashelp.class;

   if weight <&q25 then gpa='good';

    else if weight <&q75 then gpa='fine';

   else gpa='bad';

   run;

   proc print;run;

Respected Advisor
Posts: 3,799

Re: How to use IF statement with quantile

What about data loss going from floating point to character and back.

Just skip the macro variables

if _n_ eq 1 then set want;

Solution
‎10-30-2012 05:20 PM
Super User
Posts: 19,770

Re: How to use IF statement with quantile

Merging in the two variables is easier unless you need to use macro variables.

proc means data=sashelp.class noprint;

    var weight;

    output out=want(keep=qSmiley Happy q1=q1 q3=q3;

run;

data class;

    set sashelp.class ;

    if _n_=1 then set want;

    length cat $8.;

    if  0 <weight < q1 then cat='Bad';

    else if weight<=q3 then cat='Average';

    else cat='Good';

run;

Occasional Contributor
Posts: 14

Re: How to use IF statement with quantile

One thing GPA is not 100. Range from 0 to 4.

So your example code goes to all 'Bad'

How can I fix it?

PROC Star
Posts: 7,467

Re: How to use IF statement with quantile

Did you run proc means on your data file and then combine the results with your data file?

Occasional Contributor
Posts: 14

Re: How to use IF statement with quantile

I run proc means. What does it mean combine the results with my data?

FYI, original questions is

"Create a categorical equivalent of the college grade point average after three semesters (GPA) (called GRADE_CAT) as follows: bad = less than or equal to 25% quantile, average = between 25% and 75% quantile, good = more than or equal 75% quantile."

PROC Star
Posts: 7,467

Re: How to use IF statement with quantile

The following is just a restatement of Reeza's code where the student data is called test:

data test;

  input gpa;

  cards;

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1.0

1.1

1.2

1.3

1.4

1.5

1.6

1.7

1.8

1.9

2.0

2.1

2.2

2.3

2.4

2.5

2.6

2.7

2.8

2.9

3.0

3.1

3.2

3.3

3.4

3.5

3.6

3.7

3.8

3.9

4.0

;

run;

proc means data=test noprint;

    var gpa;

    output out=want(keep=qSmiley Happy q1=q1 q3=q3;

run;

data want;

    set test ;

    if _n_=1 then set want;

    length cat $8.;

    if  0 <gpa < q1 then cat='Bad';

    else if gpa <= q3 then cat='Average';

    else cat='Good';

run;

Super Contributor
Posts: 1,636

Re: How to use IF statement with quantile

0-4 is fine. Just apply your data to Reeza's code.

Occasional Contributor
Posts: 14

Re: How to use IF statement with quantile

Thanks at all : )

Super User
Posts: 10,018

Re: How to use IF statement with quantile

Or try to use proc rank.

proc rank data=sashelp.class out=want group=4;

var weight;

ranks weight_rate;

run;

Ksharp

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 422 views
  • 4 likes
  • 6 in conversation