turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- Working with military time

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

09-27-2017 12:09 PM - edited 09-27-2017 01:38 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Lavdiff

09-27-2017 01:03 PM

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).

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to mkeintz

09-27-2017 01:24 PM

will do thanks

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to mkeintz

09-27-2017 01:39 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Lavdiff

09-27-2017 01:20 PM

"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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

09-27-2017 01:25 PM

try to follow your link, it says page is not there.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Lavdiff

09-27-2017 02:14 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to mkeintz

09-27-2017 04:52 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Lavdiff

09-27-2017 05:44 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to mkeintz

09-29-2017 09:21 AM

Doing that, thank you!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Lavdiff

09-27-2017 06:38 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Lavdiff

09-27-2017 06:48 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ChrisNZ

09-29-2017 09:22 AM

thank you, and checking...