BookmarkSubscribeRSS Feed
Lavdiff
Obsidian | Level 7
Here is the code below. Time1 is wake up time and time2 is sleep time, then getuptime is subtraction of the two variables. You can see in the proc print table that is seems to be working except for some errors in the actual table. I added the treatment for missing because that was causing some issues, but something is still wrong with it, and I cannot find the solution, can please someone point me to the right direction, thank you
 
 
/*tells SAS where to find my data set*/
libname mydata '/courses/d6670be5ba27fe300' access=readonly;
options nofmterr;
 
/*tells SAS what data set to use*/
Data new; set mydata.addhealth_public4;
proc sort; by AID;/*case id*/
 
/*1st KEEP Statement tells sas what variables to look at*/
Keep AID Agegroup H4OD1M birthm H4ID5H H4ID6H H4SP1H H4SP1M H4SP1T H4SP2H H4SP2M H4SP2T H4SP3M H4SP3H H4SP3T H4SP4H H4SP4M H4SP4T H4SP5 H4SP6;
 
/*tells SAS what the older data set is to use*/
Data new2; set mydata.addhealth_pds;
 
/*2nd KEEP Statement tells sas what variables to look at*/
Keep AID bio_sex NUMETHNIC H1GI4 H1GI6A H1GI6B H1GI6C H1GI6D;
proc sort; by AID;/*case id*/
 
data withage; merge new new2;
 
/*gives us the variable along with the label (note if the variable is in your code some place then it needs to be in your label*/
Label
AID="unique identifier"
/*depression*/
H4ID5H="Have you been diagnosed with depression?" /*Categorical Nominal*/
H4ID6H="What age were you diagnosed?" /*Categorical Nominal*/
/*sleep patterns*/
H4SP1H="On the days you go to work or school, what hour do you wake up?" /*Quantitative Interval*/
H4SP1M="On the days you go to work or school, what minute do you wake up?" /*Quantitative Interval*/
H4SP1T="Do you usually wake up am or pm?" /*Categorical Nominal*/
H4SP2H="On those days, what time do you ususally go to sleep the night before (Hours)?" /*Quantitative Interval*/
H4SP2M="On those days, what minute?" /*Quantitative Interval*/
H4SP2T="On those days, what time do you usually go to sleep {pm/am}" /*Categorical Nominal*/
H4SP3M="On those days you don't have to get up at a certain time, when do you wake up {minute}?" /*Quantitative Ratio*/
H4SP3H="On those days you don't have to get up at a certain time, when do you wake up {hour}?" /*Quantitative Interval*/
H4SP3T="On those days you don't have to get up at a certain time, when do you wake up {am/pm}?" /*Categorical Nominal*/
H4SP4H="On those days, what time do you go to sleep the night or day before {hour}?" /*Quantitative Interval*/
H4SP4M="On those days, What time do you go to sleep the night or day before {minute}?" /*Quantitative Ratio*/
H4SP4T="On those days, what time do you go to sleep the night or day before {am/pm}?" /*Catigorical Nominal*/
H4SP5="How often did you have trouble falling asleep?" /*Categorical Nominal*/
H4SP6="How often did you have trouble staying asleep?" /*Categorical Nominal*/
 
 
/*demographics*/
 
H4OD1M="birth month"
bio_sex="sex of participant" /*Categorical Nominal 1=Male 2=Female*/
Agegroup="age of participant"/*Quantitative Ratio*/
Ethnicity="Ethnicity of participant" /*Categorical Nominal*/
 
 
; /*semicolon only at end of LAST label*/
 
 
 
/*subsetting to exclude people that participated in wave I but not wave IV*/
If H4OD1M GE 1 then birthm=1;
If birthm=1;
 
/*treatment for missing data*/
If bio_sex=6 then bio_sex=.;
If H4ID6H GE 97 then H4ID6H=.;
IF H4ID5H GE 6 then H4ID5H=.;
IF H4SP1H GE 96 then H4SP1H=.;
IF H4SP1M GE 96 then H4SP1M=.;
IF H4SP1T GE 6 then H4SP1T=.;
If H4SP2H GE 96 then H4SP2H=.;
If H4SP2M GE 96 then H4SP2M=.;
If H4SP2T GE 6 then H4SP2T=.;
If H4SP3H GE 96 then H4SP3H=.;
If H4SP3M GE 96 then H4SP3M=.;
If H4SP3T GE 6 then H4SP3T=.;
If H4SP4H GE 96 then H4SP4H=.;
If H4SP4M GE 96 then H4SP4M=.;
If H4SP4T GE 6 then H4SP4T=.;
 
 
 
Agegroup=2008-H4OD1Y;
 
/*creating secondary variable that is participants ethnicity (note this something that each of you
are going to want to have so you can describe your sample by ethnicity)*/
NUMETHNIC=SUM (of H1GI4 H1GI6A H1GI6B H1GI6C H1GI6D);
If NUMETHNIC GE 2 THEN ETHNICITY=1;/*MULTIPLE RACE/ETHNICITY ENDORSED*/
ELSE IF H1GI4=1 THEN ETHNICITY=2;/*HISPANIC OR LATINO*/
ELSE IF H1GI6B=1 THEN ETHNICITY=3;/*BLACK OR AFRICAN AMERICAN*/
ELSE IF H1GI6C=1 THEN ETHNICITY=4;/* AMERICAN INDIAN OR NATIVE AMERICAN*/
ELSE IF H1GI6D=1 THEN ETHNICITY=5;/*ASIAN OR PACIFIC ISLANDER*/
ELSE IF H1GI6A=1 THEN ETHNICITY=6; /*WHITE*/
ELSE ETHNICITY=7; /*OTHER*/
/*Subsetting*/
If agegroup le 28;
 
/*recode dummy codes*/
If H4ID6H NE 97 and H4ID6H=. then H4ID6H=0; /*skip*/
 
proc format ;
  picture miltime
    low-high = '%0H%0M'  (datatype=time) ;
run;
 
data new;
set mydata.addhealth_public4;
IF H4SP1H GE 96 then H4SP1H=.;
IF H4SP1M GE 96 then H4SP1M=.;
IF H4SP1T GE 6 then H4SP1T=.;
If H4SP2H GE 96 then H4SP2H=.;
If H4SP2M GE 96 then H4SP2M=.;
If H4SP2T GE 6 then H4SP2T=.;
 
  time1=hms(mod(h4sp1h,12),h4sp1m,0);
  if h4sp1t=2 then time1=time1+'12:00:00't;
 
  time2=hms(mod(h4sp2h,12),h4sp2m,0);
  if h4sp2t=2 then time2=time2+'12:00:00't;
 
  getup_time=time1-time2;
 
  format time1 time2 getup_time miltime4.;
run;
 
/*tells SAS how to sort the data by id number*/
proc sort; by AID;
/*proc print to check secondary variable*/
/*proc print; var time1 time2 getup_time;*/
/*tells SAS that I want frequency tables and for what variables*/
proc freq; tables time1 time2 getup_time; /* H4SP1H H4SP1M H4SP1T H4SP2H H4SP2M H4SP2T 
H4SP3H H4SP3M H4SP3T H4SP4H H4SP4M H4SP4T H4SP5 H4SP6 bio_sex H4ID5H H4ID6H WoScWake WoScSleep NoWoScWake NoWoScSleep */
 
/*tells SAS to analyze the data*/
run
 
attached are the pics that show the table get up table with the error
 
and the second one the log with no errors
 

table with errortable with errorlog with no errorslog with no errors

12 REPLIES 12
mkeintz
PROC Star

You forget to put in the proc print results (please paste into a box like the {i}  - "insert code" so that we can see monopitch font).

 

And please, instead of reporting "something is wrong", describe the problem (i.e. point out and unexpected result, and what you expected).

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Lavdiff
Obsidian | Level 7
will do thanks
Lavdiff
Obsidian | Level 7
but that is the problem i that i cannot find why i am getting the error in the table that show the hours, the logs shows no errors, and after that point i am lost.
ballardw
Super User

"Wrong" is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Lavdiff
Obsidian | Level 7
try to follow your link, it says page is not there.
mkeintz
PROC Star

You have 3,491 cases with getup_time=ERRO.  I presume this is the unexpected result.  If so, please print out the input data for a few of those cases.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Lavdiff
Obsidian | Level 7

Thank you but when I run a proc print; var, there is appropriate data in both variables, but an error in the new variable it created for participants.

mkeintz
PROC Star

The either the algorithm for generating the new variable, or the format used to display it, are wrong.

 

Make a sample of 2 or 3 offending records.  Run the program - but DON'T apply the format.  Remember, if you are using the user-defined format I proposed, it would not support negative values  (i.e. if you subtract the later time from the earlier time).  If your result var is negative, then you have to revisit either the format, or recalculte the var so it's never negative.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Lavdiff
Obsidian | Level 7
Doing that, thank you!
ballardw
Super User

How about providing some data just for a couple records that have "problems" and ONLY include the time related elements and code to read and process them. We do not need to see demographics and pick on the "usual" times to reduce the amount of stuff to look at.

 

Without having your data I'm willing to bet that the "minute" component of your times have 00 and 30 about equally split with 15 and 45 around half the number or 00 and 30 and that 00, 15, 30 and 45 comprise somewhere around 70 percent of your data.

ChrisNZ
Tourmaline | Level 20

Check the value of your time variables.

proc format ;
  picture miltime low-high = '%0H%0M' (datatype=time) ;
run;
data TEST;
  retain A 1e3  B 1e99  C 1e99 ;
  putlog A= miltime. B= miltime. C= miltime.;
run;

This outputs ERROR since the value is invalid but doesnt generate an error.

A=0016 B=ERROR C=ERROR

Lavdiff
Obsidian | Level 7
thank you, and checking...

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!

How to Concatenate Values

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.

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
  • 12 replies
  • 985 views
  • 3 likes
  • 4 in conversation