I need help with some longitudinal programing which I have done before!
But this time, after I assign first obs for counting observations, I get a weird result for id=55.
I included the program here (I would not accept my sas files -sorry).
- The log says it all worked but I get the wrong results.
- I attached the database - I changed dates so there are no HIPAA violations
- At end, I attached the output.
I am sorry for a basic ? but i'm tired and have spend 2 days.
IT WILL NOT ALLOW ME TO ATTACH THE DATA. SO I WILL COPY IT HERE. IT'S KIND OF LONG.
OH, IS THERE ANY WAY TO LEFT JUSTIFY THE OUTPUT?
Program:
ODS HTML close; ODS listing; /* this is SAS printout */
options linesize=132 pagesize=60 center date;
options pageno=1;
LIBNAME help 'C:\sas\a_study\post1help';
libname library2 'c:\sas\a_study\post1help';
options fmtsearch=(library2);
dm 'out; clear; log; clear;';
data one;
set help.onepost;
proc contents; proc print;
run;
data counts; /* gets me the total counts of visits for each person */
set help.onepost ; *found no duplicates;
by id;
if first.id=1 then n_visits=0;
n_visits + 1;
if last.id=1 then output;
proc print data=counts; run;
OUTPUT
The SAS System |
1 | 03/01/2018 | 03/02/2018 | 160 | 94 | 10 |
3 | 11/27/2017 | 11/28/2017 | 249 | 133 | 20 |
55 | 08/20/2010 | 08/23/2010 | 118 | 145 | 57 |
56 | 06/17/2010 | 06/18/2010 | 172 | 88 | 6 |
* Read the count (proc prints) Obs55 should be 4/30/10 for both dates!
Notice how 55 does not have its original data.
The dates are mixed up and
the first obs should be 4/30/10;
/*
Why does it change like that?
The others don't. */
* Read the count (proc prints)
Notice how 55 does not have its original data.
The dates are mixed up and
the first obs should be 4/30/10;
/*
Why does it change like that?
The others don't. */
THANKS!
Obs id initial_test_hr hr_date date prehr posthr
1 1 01/30/2018 02/19/2018 135 105
2 1 02/19/2018 02/20/2018 162 83
3 1 02/20/2018 02/21/2018 198 94
4 1 02/21/2018 02/22/2018 184 99
5 1 02/22/2018 02/23/2018 153 105
6 1 02/23/2018 02/26/2018 161 127
7 1 02/26/2018 02/27/2018 134 148
8 1 02/27/2018 02/28/2018 174 129
9 1 02/28/2018 03/01/2018 206 152
10 1 03/01/2018 03/02/2018 160 94
11 3 10/05/2017 10/30/2017 137 160
12 3 10/30/2017 10/31/2017 162 109
13 3 10/31/2017 11/01/2017 305 176
14 3 11/01/2017 11/02/2017 207 211
15 3 11/02/2017 11/03/2017 175 96
16 3 11/03/2017 11/06/2017 145 78
17 3 11/06/2017 11/07/2017 172 175
18 3 11/07/2017 11/08/2017 186 95
19 3 11/08/2017 11/09/2017 261 173
20 3 11/09/2017 11/10/2017 148 118
21 3 11/10/2017 11/13/2017 166 134
22 3 11/13/2017 11/14/2017 197 157
23 3 11/14/2017 11/15/2017 351 162
24 3 11/15/2017 11/16/2017 231 100
25 3 11/16/2017 11/17/2017 235 100
26 3 11/17/2017 11/20/2017 126 88
27 3 11/20/2017 11/21/2017 144 113
28 3 11/21/2017 11/22/2017 241 268
29 3 11/22/2017 11/27/2017 129 153
30 3 11/27/2017 11/28/2017 249 133
31 55 04/20/2012 04/20/2010 154 128
32 55 04/20/2010 05/10/2010 164 113
33 55 05/10/2010 05/11/2010 135 107
34 55 05/11/2010 05/12/2010 138 112
35 55 05/12/2010 05/13/2010 109 119
36 55 05/13/2010 05/14/2010 155 107
37 55 05/14/2010 05/19/2010 189 149
38 55 05/19/2010 05/21/2010 235 138
39 55 05/21/2010 05/24/2010 190 201
40 55 05/24/2010 05/25/2010 181 116
41 55 05/25/2010 05/26/2010 190 165
42 55 05/26/2010 05/27/2010 177 170
43 55 05/27/2010 05/28/2010 158 111
44 55 05/28/2010 06/01/2010 188 116
45 55 06/01/2010 06/02/2010 230 100
46 55 06/02/2010 06/08/2010 127 120
47 55 06/08/2010 06/09/2010 187 132
48 55 06/09/2010 06/10/2010 196 167
49 55 06/10/2010 06/11/2010 193 149
50 55 06/11/2010 06/15/2010 150 81
51 55 06/15/2010 06/16/2010 165 119
52 55 06/16/2010 06/17/2010 172 128
53 55 06/17/2010 06/18/2010 105 123
54 55 06/18/2010 06/21/2010 208 107
55 55 06/21/2010 06/22/2010 163 85
56 55 06/22/2010 06/23/2010 180 98
57 55 06/23/2010 06/24/2010 196 81
58 55 06/24/2010 06/25/2010 202 118
59 55 06/25/2010 06/29/2010 183 85
60 55 06/29/2010 06/30/2010 181 147
61 55 06/30/2010 07/01/2010 151 119
62 55 07/01/2010 07/02/2010 184 154
63 55 07/02/2010 07/06/2010 199 93
64 55 07/06/2010 07/08/2010 191 101
65 55 07/08/2010 07/09/2010 187 94
66 55 07/09/2010 07/12/2010 36 156
67 55 07/12/2010 07/13/2010 146 96
68 55 07/13/2010 07/19/2010 267 130
69 55 07/19/2010 07/21/2010 152 99
70 55 07/21/2010 07/23/2010 265 104
71 55 07/23/2010 07/26/2010 164 158
72 55 07/26/2010 07/27/2010 181 93
73 55 07/27/2010 07/28/2010 250 111
74 55 07/28/2010 07/30/2010 251 104
75 55 07/30/2010 08/03/2010 178 125
76 55 08/03/2010 08/04/2010 224 93
77 55 08/04/2010 08/05/2010 195 145
78 55 08/05/2010 08/06/2010 246 123
79 55 08/06/2010 08/10/2010 155 121
80 55 08/10/2010 08/11/2010 307 91
81 55 08/11/2010 08/12/2010 179 106
82 55 08/12/2010 08/13/2010 258 207
83 55 08/13/2010 08/16/2010 198 77
84 55 08/16/2010 08/17/2010 208 123
85 55 08/17/2010 08/19/2010 225 155
86 55 08/19/2010 08/20/2010 206 115
87 55 08/20/2010 08/23/2010 118 145
88 56 06/01/2010 06/09/2010 151 91
89 56 06/09/2010 06/10/2010 189 108
90 56 06/10/2010 06/15/2010 156 94
91 56 06/15/2010 06/16/2010 162 107
92 56 06/16/2010 06/17/2010 183 116
93 56 06/17/2010 06/18/2010 172 88
I see no problem. You get all 4 variable values from obs 87 of onepost, and the correct count of 57 (obs 31 to 87).
I think I send the wrong part of the dataset for id=55. let me look again.
I posted the wrong data but the right out put.
If you notice id=55, the 1st visitnumber should be that where there is 4/20/10 and 4/20/10. And it is not. I copied it on a word document this time.
Feel free to test my program and see what you get.
I can't get the 1st visit to be the 1st observation for id 55, which should be 4/20/10 and 4/20/10. That person had the 1st initial consult on 4/20/10 and went to the treatment to treat the heart rate the same day.
I APOLOGIZE greatly if I got the data mixed up.
But this is the correct data here.
It is in excel. I hope it attaches. I don't know how to make these things attach. I tried everything!
I have to take a nap. I am so tired. Please help me.
You mentioned DATES but your posted code is doing NOTHING with the date variables. What do you want to do with the date variables?
Let's assume that in addition to counting observations you want to find the first and the last date per ID.
data want ;
set help.onepost ;
by id date ;
retain n_visits first_date ;
format first_date last_date date9. ;
if first.id then n_visits=0;
if first.id then first_date = date ;
n_visits + 1;
if last.id ;
last_date = date ;
run;
Make sure that your retained variable (N_VISITS and FIRST_DATE) are NOT already on the input dataset. If they are then either use different variable names or add the DROP= dataset option to the SET statement so the old variables are not read into the current step.
PS Learn to use the tools on the Forum editor menu to paste text, programs and images. This will make your postings easier to read.
Hi Tom,
I tested your code. I do not understand the date comment. I have:
initial_hr_date and test_hr_date.
It gets stuck when sorting by id date - I hv no date variable.
Do I need to change initial_hr_date to firstdate and test_hr_date to last_date?
Could you write it out? I am so confused.
Thanks.
Tom, I get this in my log:
BY variable date is not on input data set HELP.ONEPOST.
@katemcguckin wrote:
Tom, I get this in my log:
BY variable date is not on input data set HELP.ONEPOST.
If you want code that has been tested against real SAS data, you have to post such. Excel spreadsheets are useless for this. They only leave us guessing how your SAS datasets might look.
Post data in a data step with datalines, and we have something to work with.
Adapt the concepts to your data. You seem to have two date variables. What do you want to do with them? If you do nothing, like in your original post, then you will output the values that are on the last observation for the group.
Hi,
You know. It dawned on me when I use the long data and program it, it's all ok. I think I confused myself so much.
I want to know tge days between treatmeants time from 1st visit to last visit,
I did those using code from Cody's book.
I am sorry I bothered you. I sometimes over think and confuse myself.
Thanks for your time, though! I am sorry.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.