BookmarkSubscribeRSS Feed
tc
Lapis Lazuli | Level 10 tc
Lapis Lazuli | Level 10

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;
7 REPLIES 7
SteveBittner
Fluorite | Level 6

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

 

 

 

 

tc
Lapis Lazuli | Level 10 tc
Lapis Lazuli | Level 10

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>

 

SteveBittner
Fluorite | Level 6

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;

LaurieF
Barite | Level 11

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?

SteveBittner
Fluorite | Level 6

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

LaurieF
Barite | Level 11

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.

SteveBittner
Fluorite | Level 6

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

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
  • 7 replies
  • 2929 views
  • 6 likes
  • 3 in conversation