BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lidia1
Obsidian | Level 7
subjid                              DOSE   NAM   VAL
3120MG FeSHEIGHTNO167.2
7120MG FeSHEIGHTNO170
2120MG FeSHEIGHTNO172.7
10120MG FeSHEIGHTNO173
6120MG FeSHEIGHTNO176
9120MG FeSHEIGHTNO53
11240MG FeSHEIGHTNO159
14240MG FeSHEIGHTNO164
13240MG FeSHEIGHTNO169
1240MG FeSHEIGHTNO175
17240MG FeSHEIGHTNO182.8
12240MG FeSHEIGHTNO74
22360MG FeSHEIGHTNO160.5
20360MG FeSHEIGHTNO166
25360MG FeSHEIGHTNO166
18360MG FeSHEIGHTNO171
19360MG FeSHEIGHTNO177.8
15360MG FeSHEIGHTNO193

 

My desired output is 

 

Doseminmax
120MG FeS54167.2
240MG FeS74159
360MG FeS160.5193

 

Kindly help me to frame this. I am always getting the max value as a min value.

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @Lidia1,

 

Step 1: Create a numeric variable VAL from the existing character variable VAL (or QVAL), e.g., like this:

data have2;
set have(rename=(val=valc));
val=input(valc,32.);
run;

Step 2: Apply any of the suggested solutions (or possibly your existing code) to the new dataset (HAVE2 in the example above).

View solution in original post

15 REPLIES 15
novinosrin
Tourmaline | Level 20
proc sql;
create table want as
select dose, min(val) as min, max(val) as max
from your_dataset
group by dose;
quit;
Lidia1
Obsidian | Level 7

I have used your query --

proc sql;
create table want as
select exdose, min(qval) as min, max(qval) as max
from exhgt
group by exdose;
quit;

 

But again it is giving me the  below result which is not correct--

 

doseminmax
120MG FeS167.253
240MG FeS15974
360MG FeS160.5193
PaigeMiller
Diamond | Level 26

@Lidia1 wrote:

I have used your query --

proc sql;
create table want as
select exdose, min(qval) as min, max(qval) as max
from exhgt
group by exdose;
quit;

 

But again it is giving me the  below result which is not correct--

 

dose min max
120MG FeS 167.2 53
240MG FeS 159 74
360MG FeS 160.5 193

There's something wrong above, because I get the desired results using PROC SQL or PROC SUMMARY. Either your data set is not represented properly, or your code as shown is not the code you are running.

 

Can you please provide the data as a SAS data step as shown below, and also the EXACT unedited code you are using? Copy and paste (do not type) the EXACT code into a code box (click on the running man icon to open the code box).

 

data have;
infile cards;
input subjid DOSE &:$9.	  NAM $9.	   VAL;
cards;
3	120MG FeS	HEIGHTNO	167.2
7	120MG FeS	HEIGHTNO	170
2	120MG FeS	HEIGHTNO	172.7
10	120MG FeS	HEIGHTNO	173
6	120MG FeS	HEIGHTNO	176
9	120MG FeS	HEIGHTNO	53
11	240MG FeS	HEIGHTNO	159
14	240MG FeS	HEIGHTNO	164
13	240MG FeS	HEIGHTNO	169
1	240MG FeS	HEIGHTNO	175
17	240MG FeS	HEIGHTNO	182.8
12	240MG FeS	HEIGHTNO	74
22	360MG FeS	HEIGHTNO	160.5
20	360MG FeS	HEIGHTNO	166
25	360MG FeS	HEIGHTNO	166
18	360MG FeS	HEIGHTNO	171
19	360MG FeS	HEIGHTNO	177.8
15	360MG FeS	HEIGHTNO	193
run;

proc sql;
create table want as
select dose, min( val) as min, max( val) as max
from have
group by dose;
quit;

Also, does the variable VAL have some formatting??? Can you show us the PROC CONTENTS output for your SAS data set?

--
Paige Miller
Lidia1
Obsidian | Level 7

Yes. PFB the proc contents -

 
obsVariableTypeLenFormatInformatLabel
2EXDOSEChar9$9.00$9.00EXDOSE
3QNAMChar20$18.00$18.00QNAM
4QVALChar24$24.00$24.00QVAL
1subjidNum8   

 

I have also shared (below)the data which I am using.

 

ObssubjidEXDOSEQNAMQVAL
13120MG FeSHEIGHTNO167.2
27120MG FeSHEIGHTNO170
32120MG FeSHEIGHTNO172.7
410120MG FeSHEIGHTNO173
56120MG FeSHEIGHTNO176
69120MG FeSHEIGHTNO53
711240MG FeSHEIGHTNO159
814240MG FeSHEIGHTNO164
913240MG FeSHEIGHTNO169
101240MG FeSHEIGHTNO175
1117240MG FeSHEIGHTNO182.8
1212240MG FeSHEIGHTNO74
1322360MG FeSHEIGHTNO160.5
1420360MG FeSHEIGHTNO166
1525360MG FeSHEIGHTNO166
1618360MG FeSHEIGHTNO171
1719360MG FeSHEIGHTNO177.8
1815360MG FeSHEIGHTNO193
ed_sas_member
Meteorite | Level 14

Hi @Lidia1 

 

According to your data, min and max values in each group are:

Obs subjid EXDOSE QNAM QVAL
1 3 120MG FeS HEIGHTNO 167.2
2 7 120MG FeS HEIGHTNO 170
3 2 120MG FeS HEIGHTNO 172.7
4 10 120MG FeS HEIGHTNO 173
5 6 120MG FeS HEIGHTNO 176
6 9 120MG FeS HEIGHTNO 53
7 11 240MG FeS HEIGHTNO 159
8 14 240MG FeS HEIGHTNO 164
9 13 240MG FeS HEIGHTNO 169
10 1 240MG FeS HEIGHTNO 175
11 17 240MG FeS HEIGHTNO 182.8
12 12 240MG FeS HEIGHTNO 74
13 22 360MG FeS HEIGHTNO 160.5
14 20 360MG FeS HEIGHTNO 166
15 25 360MG FeS HEIGHTNO 166
16 18 360MG FeS HEIGHTNO 171
17 19 360MG FeS HEIGHTNO 177.8
18 15 360MG FeS HEIGHTNO 193

 

So this doesn't correspond to your expected output ???

Lidia1
Obsidian | Level 7

No. I am getting min value as max in my output dataset.

brzcol
SAS Employee

This is probably because the val column is character. You will need this to be numeric. See the solution below another user made to go from character to numeric.

FreelanceReinh
Jade | Level 19

@Lidia1 wrote:

No. I am getting min value as max in my output dataset.


This just seems to be the case for some groups because, e.g., "53">"176" alphabetically -- and alphabetical order is relevant when comparing character values (e.g. via MIN and MAX functions in PROC SQL). Please convert QVAL to numeric using the INPUT function as suggested earlier.

PaigeMiller
Diamond | Level 26
proc summary data=have nway;
    class dose;
    var val;
    output out=want min=min max=max;
run;
--
Paige Miller
brzcol
SAS Employee

One option is to use proc means:

 

proc means data=ExampleTable min max nonobs;
   class dose;
   var val;
run;

means.PNG

data_null__
Jade | Level 19

I like PROC SUMMARY for this and it has features to ID the obs that is MIN and MAX.  In this example I use it find the subject with MIN/MAX in each group.

 

 

data sl;
   input subjid $ DOSE &$10. NAM :$32. val;
   cards;
3
120MG FeS
HEIGHTNO
167.2
7
120MG FeS
HEIGHTNO
170
2
120MG FeS
HEIGHTNO
172.7
10
120MG FeS
HEIGHTNO
173
6
120MG FeS
HEIGHTNO
176
9
120MG FeS
HEIGHTNO
53
11
240MG FeS
HEIGHTNO
159
14
240MG FeS
HEIGHTNO
164
13
240MG FeS
HEIGHTNO
169
1
240MG FeS
HEIGHTNO
175
17
240MG FeS
HEIGHTNO
182.8
12
240MG FeS
HEIGHTNO
74
22
360MG FeS
HEIGHTNO
160.5
20
360MG FeS
HEIGHTNO
166
25
360MG FeS
HEIGHTNO
166
18
360MG FeS
HEIGHTNO
171
19
360MG FeS
HEIGHTNO
177.8
15
360MG FeS
HEIGHTNO
193
;;;;
   run;
proc print;
   run;

proc summary data=sl nway missing;
   class nam dose;
   output out=stats(drop=_type_)
      idgroup(max(val) out(val subjid)=max maxsubj)
      idgroup(min(val) out(val subjid)=min minsubj)
      ;
   run;
proc print;
   run;

image.png

FreelanceReinh
Jade | Level 19

Hi @Lidia1,

 

Step 1: Create a numeric variable VAL from the existing character variable VAL (or QVAL), e.g., like this:

data have2;
set have(rename=(val=valc));
val=input(valc,32.);
run;

Step 2: Apply any of the suggested solutions (or possibly your existing code) to the new dataset (HAVE2 in the example above).

PaigeMiller
Diamond | Level 26

So @Lidia1 (and others), the lesson here is that you can't perform math (find a minimum or maximum or any other type of statistic or calculation) on character variables. Maxim 3 — know your data — I've heard that somewhere before.

 

Had you used my preference which is PROC SUMMARY (or PROC MEANS), you would have received an error in the log, alerting you to the problem, which would then have led to a fix. You would not have received incorrect results. PROC SQL doesn't produce an error; which is another reason why I usually don't use PROC SQL for math.

--
Paige Miller
Lidia1
Obsidian | Level 7

Yes. I tried your query. But I got Type mismatch (in by variable i.e. dose) error at my end. 

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
  • 15 replies
  • 13564 views
  • 20 likes
  • 7 in conversation