BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
newmkka
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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=q:) 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

10 REPLIES 10
Linlin
Lapis Lazuli | Level 10

a sample code:

/* get Q25 and Q75 */

proc means data=sashelp.class noprint;

var weight;

output out=want(keep=q:) 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;

data_null__
Jade | Level 19

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

Just skip the macro variables

if _n_ eq 1 then set want;

Reeza
Super User

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=q:) 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;

newmkka
Calcite | Level 5

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

So your example code goes to all 'Bad'

How can I fix it?

art297
Opal | Level 21

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

newmkka
Calcite | Level 5

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

art297
Opal | Level 21

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=q:) 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;

Linlin
Lapis Lazuli | Level 10

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

newmkka
Calcite | Level 5

Thanks at all : )

Ksharp
Super User

Or try to use proc rank.

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

var weight;

ranks weight_rate;

run;

Ksharp

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 10 replies
  • 1474 views
  • 4 likes
  • 6 in conversation