BookmarkSubscribeRSS Feed
ugly_duck_ling
Calcite | Level 5

dm log 'clear';

libname hosp 'D:\sas';

data labs;
input id prelab1 prelab2 postlab1 postlab2;
datalines;
1 9.8 318 10.9 215
2 . . . 129
3 . . 5.7 136
4 7.8 124 . .
5 . . 6.6 123
6 . . . 127
7 . . 5.8 127
8 7.9 196 . .
9 . . 4.9 134
10 . . . 131
11 . 171 . .
12 7.1 140 7.1 88
13 . 160 . 135
14 6.9 107 6.5 154
15 . . 6.6 122
16 . 127 . .
17 6.5 102 . .
18 . 149 . .
19 . . 6.6 120
20 . . . 129
21 . 128 . .
22 5.5 146 5.3 136
23 . . 6.5 .
24 6.6 127 6.3 130
25 . 155 . 133
26 7 145 . .
27 . . 8.1 148
28 6.6 103 . .
29 6.8 140 6.5 124
30 . 143 . .
31 . . 5.7 128
32 6.6 156 7.2 .
33 . . . 140
34 . . . 134
35 . . 6.5 111
36 . . . 137
37 5.7 127 . .
38 . 148 6.3 142
39 . 173 6.8 150
40 5.7 163 . .
41 . 150 . .
42 . 167 . .
43 8.6 184 6.5 .
44 8.2 113 7.8 173
45 24 . 144 . .
46 . . . 126
47 6.5 109 6.9 115
48 7.7 184 7.8 190
49 . 158 . .
50 . . . 127
51 . 146 . .
52 . . 5.9 128
53 . 127 5.9 131
54 9.6 198 7 84
55 . . 7 93
56 6 133 . .
57 6.7 185 7.5 204
58 6.3 165 . 136
59 4.9 150 . 137
60 6.9 130 6.9 127
61 6.3 137 7.1 166
62 . 138 . 149
63 5.5 160 5.7 152
64 12.5 592 5.5 147
65 . . . 148
66 . . . 161
67 7.4 162 6.5 204
68 9 255 5.8 150
69 7.3 141 7.2 128
70 7.5 . 7.7 198
71 6.9 173 . .
72 . . 6.9 150
73 6.3 135 6.3 164
74 8.5 110 8.3 156
75 6.7 121 7 125
76 . . 5.7 132
77 . . . 154
78 9.6 201 8 .
79 . 138 . .
80 . 131 . .
81 . 183 . 141
82 . . . 185
83 6.3 145 . .
84 . 131 . 134
85 . 185 6.5 122
86 . . . 201
87 7.8 160 8.3 213
88 5.6 138 . .
89 10.9 114 8.9 178
90 . . 6.6 105
91 . . . 147
92 6.7 103 6 138
93 . . . 139
94 . 137 . .
95 . . . 134
96 . . 6.2 161
97 9 191 7.3 156
98 6.7 107 . .
99 . . 9.1 127
100 . . 6.6 119
;
run;
proc print;
run;

/*I have a dataset of the lab values of two tests before and after taking a certain drug. Using a single macro, I need
to do three things:

1. remove missing values for the changes in these lab values (post-pre). That is changelab1 and changelab2
2. classify the patients in this sample into three groups:
Group A: Those with prelab1 greater than or equal 6
Group B: Those with prelab2 greater than or equal 130
Group C: Those with prelab1 >= 6 and prelab2 >= 130
3. do proc univariate on each of each of the three groups to find the number of patients who have done each lab test, and the mean and standard deviation of each lab test for each group. So, I need to get finally:

The number of people who have done lab1 in groups A, B, and C (pre and post). Supposably they are equal.
The mean value of lab1 and lab2 values (pre and post) in the three groups.
The standard deviation of lab1 and lab2 values (pre and post) in the three groups.

 

Is that possible using a single macro?

5 REPLIES 5
PaigeMiller
Diamond | Level 26

You can do all of this without a macro at all. Why do you feel a macro is needed?

--
Paige Miller
ugly_duck_ling
Calcite | Level 5
For efficiency in calculating those if needed.
PaigeMiller
Diamond | Level 26

Macros are just a way of writing dynamic code. The efficiency of doing calculations is not improved by a macro, the code (for example, PROC UNIVARIATE) executes at the same speed regardless of whether or not a macro is used.


What does "remove missing values" mean to you? Does it mean remove those observations entirely, or just do not use the missings, or something else?

--
Paige Miller
ugly_duck_ling
Calcite | Level 5
remove the observations entirely.
PaigeMiller
Diamond | Level 26

Here is code to do this using PROC SUMMARY instead of PROC UNIVARIATE. The output data set which I call _STATS_ contains what you want.

 

proc format;
    value lab1f low-<6='PreLab1 < 6' 6-high='PreLab1 >= 6';
    value lab2f low-<130='PreLab2 < 130' 130-high = 'PreLab2 >= 130';
run;
data want;
    set labs;
    changelab1=postlab1-prelab1;
    changelab2=postlab2-prelab2;
    if changelab1=. then call missing(prelab1,postlab1);
    if changelab2=. then call missing(prelab2,postlab2);
    format prelab1 lab1f. prelab2 lab2f.;
run;
proc summary data=want;
    class prelab1 prelab2;
    ways 1 2;
    var prelab1 prelab2 postlab1 postlab2 changelab1 changelab2;
    output out=_stats_ n= mean= stddev=/autoname noinherit;
run;
--
Paige Miller

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
  • 5 replies
  • 494 views
  • 0 likes
  • 2 in conversation