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

Was asked the following question:

2) Create a new variable called HospitalDays that contains the number of days between 
date of hospitalization and date of either discharge or death.
 (Hint: SAS dates are stored as days already, so the difference is already expressed in terms 
 of days.)

 used the following code:

data hospitaldays;
set cohortwide;
HospitalDays = LastVisitDate death ;
run;

 and all of my data is missing. What am I doing wrong here? I followed the steps my professor gave.

Then was later asked this:

3) Create a new variable called Age that is the number of years of age the person
was at the time of symptom onset. (For this, you should try to use the function
shown in the lab demonstration.

 Used this code:

data hospitaldays;
Age = event ;
run;

 I'm certain that I'm doing something very wrong and may have missed some steps but am wondering how to correct them

saza_0-1645796798922.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data hospitaldays;
set cohortwide;
HospitalDays = LastVisitDate death ;
run;

Maxim 2 — Read the LOG. Surely, the LOG shows errors or warnings here.

 

data hospitaldays;
Age = event ;
run;

The question asks you to use a function from the lab. You have not used any function here, and of course I don't know what function was used in the lab (although I could guess, I am assuming you know what function was used in the lab, so no point in me guessing).

 

Furthermore, you are saying that the value of age is the value of event. Please consider the question ... does that make sense? If you had to do this, and you needed to calculate age when an event happened, explain in words (not with SAS or other code) how to calculate the age.

--
Paige Miller

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26
data hospitaldays;
set cohortwide;
HospitalDays = LastVisitDate death ;
run;

Maxim 2 — Read the LOG. Surely, the LOG shows errors or warnings here.

 

data hospitaldays;
Age = event ;
run;

The question asks you to use a function from the lab. You have not used any function here, and of course I don't know what function was used in the lab (although I could guess, I am assuming you know what function was used in the lab, so no point in me guessing).

 

Furthermore, you are saying that the value of age is the value of event. Please consider the question ... does that make sense? If you had to do this, and you needed to calculate age when an event happened, explain in words (not with SAS or other code) how to calculate the age.

--
Paige Miller
saza
Quartz | Level 8
Honestly when it comes to the question about the lab function I should be able to figure it out as long as I can figure out how to write my first code correctly. I included a pic of my lab handout In the original question.

When I type in the code:
data hospitaldays;
set cohortwide;
HospitalDays = date (death-hospitalized) ;
run;
to get a dataset called "Hospitaldays" from the cohortwide data set, I see that I need to create a new variable called HospitalDays which is equal to the the number of days between the hospital stay and time of death.

I am just having trouble going about this and when I run the code and view my log it says " ERROR 72-185: The DATE function call has too many arguments."
PaigeMiller
Diamond | Level 26

@saza wrote:
Honestly when it comes to the question about the lab function I should be able to figure it out as long as I can figure out how to write my first code correctly. I included a pic of my lab handout In the original question.

This screen capture does not mention any function. No, you don't need to first figure out how to write your first code correctly to know what function was mentioned in the lab. I'm sure you must know what function was mentioned, so tell us, what function was mentioned.

 


When I type in the code:
data hospitaldays;
set cohortwide;
HospitalDays = date (death-hospitalized) ;
run;
to get a dataset called "Hospitaldays" from the cohortwide data set, I see that I need to create a new variable called HospitalDays which is equal to the the number of days between the hospital stay and time of death.

I am just having trouble going about this and when I run the code and view my log it says " ERROR 72-185: The DATE function call has too many arguments."

So this is a good place to take a step back and discuss the actual data. If the actual data is dates (in other words, variable DEATH is an actual date and also variable HOSPITALIZED is an actual date, and it appears they are based upon the screen capture you provided) then you simply need to do a subtraction to get the number of days between event and date of hospitalization to get the value of HOSPITALDAYS. Try doing this subtraction without any function whatsoever.

 

You have avoided the question about AGE at symptom onset. Without discussing any computer code, how would you do this calculation? If you had to do it pencil and paper, please tell us the formula you would use in words.

--
Paige Miller
saza
Quartz | Level 8
I think one thing that I will work on next time is properly adding all the information in my question so that way the viewer is not confused. This didn't answer my question but was close enough. I learned through a class mate that I am able to do this using the IF THEN functions. I will try that out and if that doesn't work I'll ask the question again with the correct information. Thanks again Paige
Tom
Super User Tom
Super User

There are no IF() or THEN() functions in SAS.

There is an IF ... THEN ... STATEMENT.

There are also the IFN() and IFC() functions.

saza
Quartz | Level 8
Hi Tom,

I was briefly able to make this work but for some reason it didn't work when I ran the code:
Started off with this code to make the long dataset wide (might have messed up here):
proc transpose data = covidlng.covidlong out = cohortwide prefix = Event;
by id lastname dob;
id event;
var date;
run;
Used this to add the hospitaldays variable:
DATA HospitalDays;
Set cohortwide;
if Date_Discharge ~= . then HospitalDays = Date_Discharge - Date_Hospitalized;
else HospitalDays = Date_Death - Date_Hospitalized;
run;

I get the chart just no values under the new variables, a bunch of .
Tom
Super User Tom
Super User

Where is DATE_DISCHARGE and all of those other DATE... variables going to come from?  You did not include that variable when you made COHORTWIDE.

The only variables you created are id lastname dob and however many EVENTxxxx variables you created.

 

Is DATE_DISCHARGE one of the values of EVENT?  If so then remove the PREFIX=EVENT option from the PROC TRANSPOSE.  With that the variable name would be EVENTDATE_DISCHARGE instead of DATE_DISCHARGE.

 

What is ~= ?  are you trying to test for missing.  Use the MISSING() function and the NOT operator and you code will be clearer

if not missing(date_discharge) then ...

Or perhaps skip the IF and use the MIN() or COALESCE() function instead?

HospitalDays = coalesce(Date_Discharge,Date_Death) - Date_Hospitalized;

Some times it helps to add one so that an admission and discharge on the same day counts as one day instead of zero days.  An admission on one day and discharge the next is two days (the day of admission and the day of discharge).  Etc.

saza
Quartz | Level 8
I completely understand your point, list variable Date_discharge next to prefix, however, I'm not trying to create a new variable called "date_discharged" i'm trying to tell sas to use the date value of discharged and the date value of hospitalized to be subtracted and that subtracted value needs to be shown under a new variable called HospitalDays
Tom
Super User Tom
Super User

Run a proc contents on the dataset you created in the PROC TRANSPOSE step so you can see the names of the variables.

From this photograph you posted  https://communities.sas.com/t5/image/serverpage/image-id/68841iE5F4C4B6C62281DA/image-size/medium?v=...(why post photographs of text?) you could not have gotten a variable named date_discharge.  The variable will be named DISCHARGE or EVENTDISCHARGE

PaigeMiller
Diamond | Level 26

@saza wrote:
... however, I'm not trying to create a new variable called "date_discharged"

Sometimes, intermediate variables are created to help in the coding or help in the calculations or help in the debugging. If you don't want the variable, you can delete it any time you want.

--
Paige Miller
Reeza
Super User

Attention to detail is very important in programming, every semicolon matters. 

 

In this case you forgot at least a subtraction/minus symbol in the statement. 

If you list two variables how does SAS know what to do with them?

 

TIP: You can avoid issues like this by commenting your code to state what you're planning to do. Then do it in the code. Then check it in the log AND data. 

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 1195 views
  • 7 likes
  • 4 in conversation