Data visualization with SAS programming

Steppin' Out: Getting Step Data Out of the iPhone Health App and Into SAS

Reply
Contributor tc
Contributor
Posts: 74

Steppin' Out: Getting Step Data Out of the iPhone Health App and Into SAS

SGF2016steps.png

 

If you're Fitbit-less but have an iPhone, you can still follow-in-the-footsteps of Rick Wicklin and analyze every step you take. Just email yourself a .zip file of your step data using the iPhone Health App Export feature, uncompress the data, and read the export.xml file into SAS. Once it's there, you can of course have-at-it with all things SAS!

 

* Step plot of steps tracked by iPhone Health App at SAS Global Forum 2016
  See idownloadblog.com/2015/06/10/how-to-export-import-health-data/ for how to get step data;

filename health '/folders/myfolders/export.xml';  * XML data provided by iPhone Health App;
filename map '/folders/myfolders/HealthData.map';
libname health xmlv2 xmlmap=map automap=replace;

proc sql;                                         * Grab step data from iPhone;
create table steps as
select input(substr(record_startDate,1,19),YMDDTTM19.) as startDT,
       input(substr(record_endDate,1,19),YMDDTTM19.) as endDT, record_value as steps
from health.Record(where=(record_type="HKQuantityTypeIdentifierStepCount"))
where record_startDate between '2016-04-18' and '2016-04-22 05' order by 1;

data totSteps(keep=time totsteps);                * Add cumulative steps for each day;
retain totsteps 0;                                
set steps;                                        * Reset cumulative steps if "new morning";
if intck('hour',lag1(enddt),startdt)>4 then totsteps=0; 
time=startdt;                                     * Time and total steps at beginning of period;
output;
totsteps+steps;                                   * Time and total steps at end of period;
time=enddt;
format time datetime19.;
output;
                                                  * Plot steps with a (what else?) step plot;
ods listing image_dpi=300 gpath='/folders/myfolders';
ods graphics on / reset antialias width=14in height=8.5in imagename="SGF2016steps";
proc sgplot data=totsteps;
title "Steps Taken at SGF 2016";
step x=time y=totsteps; 
yaxis label="STEPS (Source: iPhone Health App)" valuesformat=comma9. grid;
xaxis type=time grid valuesformat=datetime14. label="TIME (CDT)" fitpolicy=stagger
      values=("18APR2016:00:00:00"dt to "22APR2016:06:00:00"dt by "06:00:00"t) notimesplit;  
run;
Occasional Contributor
Posts: 7

Re: Steppin' Out: Getting Step Data Out of the iPhone Health App and Into SAS

What I'm missing is how to get the map file used in the libname statement?

 

 

 

 

Contributor tc
Contributor
Posts: 74

Re: Steppin' Out: Getting Step Data Out of the iPhone Health App and Into SAS

[ Edited ]

I had actually hand-coded a short MAP (see below), but then decided to try the AUTOMAP option, which amazingly generates a MAP for you in the file whose name you specify, and went with that look-Ma-no-code approach instead!

 

HAND-CODED MAP

<?xml version="1.0" ?>
<SXLEMAP version="1.2">
<TABLE name="RECORDS"> 
<TABLE-PATH syntax="XPATH">/HealthData/Record</TABLE-PATH>
<COLUMN name="startDate"><PATH>/HealthData/Record@startDate</PATH><TYPE>character</TYPE><DATATYPE>STRING</DATATYPE><LENGTH>40</LENGTH></COLUMN>
<COLUMN name="endDate"><PATH>/HealthData/Record@endDate</PATH><TYPE>character</TYPE><DATATYPE>STRING</DATATYPE><LENGTH>40</LENGTH></COLUMN>
<COLUMN name="type"><PATH>/HealthData/Record@type</PATH><TYPE>character</TYPE><DATATYPE>STRING</DATATYPE><LENGTH>100</LENGTH></COLUMN>
<COLUMN name="unit"><PATH>/HealthData/Record@unit</PATH><TYPE>character</TYPE><DATATYPE>STRING</DATATYPE><LENGTH>40</LENGTH></COLUMN>
<COLUMN name="value"><PATH>/HealthData/Record@value</PATH><TYPE>numeric</TYPE><DATATYPE>integer</DATATYPE></COLUMN>
</TABLE>
</SXLEMAP>

 

Occasional Contributor
Posts: 7

Re: Steppin' Out: Getting Step Data Out of the iPhone Health App and Into SAS

Thank you,

Your suggetion to use automap=replace worked like a charm.

 

filename xmldoc '/yourdisk/export.xml';

filename map '/yourdisk/healthdata.map';

libname xmldoc xmlv2 automap=replace xmlmap=map;

proc datasets lib=xmldoc;

run;

Super Contributor
Posts: 251

Re: Steppin' Out: Getting Step Data Out of the iPhone Health App and Into SAS

Hmm - I can't get this to work. I know I have write access to the folder, because I can create datasets and flat files in it programmatically; but there's no other hint as to what the error message might be.

 

57         filename health '/folders/myfolders/apple_health_export/export.xml';
 58         
 59         filename map '/folders/myfolders/apple_health_export/HealthData.map';
 60         
 61         libname health xmlv2 xmlmap=map automap=replace;
 ERROR: The creation of the XML Mapper file failed.
 ERROR: Error in the LIBNAME statement.

<scratches head>

 

Any ideas?

Occasional Contributor
Posts: 7

Re: Steppin' Out: Getting Step Data Out of the iPhone Health App and Into SAS

I don'see the error.  What version of SAS are you using?  I'm using 9.4. I've since found an iPhone app the writes a .csv file from Health Data.  The app is called QS ACCESS and is free from Apple Store

Super Contributor
Posts: 251

Re: Steppin' Out: Getting Step Data Out of the iPhone Health App and Into SAS

SAS Studio. I can get the XML mapping to work with a simpler, smaller file. And if I create an XML file from a SAS dataset and map that, it works.

 

I was wondering if it were to do with the complexity of all the sub-files within the Health app export.

Occasional Contributor
Posts: 7

Re: Steppin' Out: Getting Step Data Out of the iPhone Health App and Into SAS

Very likely size is an issue.  You might try the map file coded by TC above. It may skip most of the complexity 

Ask a Question
Discussion stats
  • 7 replies
  • 1279 views
  • 6 likes
  • 3 in conversation