Hello @jkeighley ,
Below I have some code that needs some debugging, but should be a solid place to start. Any other scripters that could improve the script below? I'm sure there is much improvement needed!
- Opens US-County-Name.jmp & preps for searching later, by breaking apart state and county into new columns
- Downloads & opens the PDF you linked above, then imports the table on the first page
- Note - always review script before running on your local machine! This script downloads a PDF from the internet. Check line 15, the URL string in the URL() argument
- Assigns creates an associative array (AA) of regions (keys) & counties (values) within the region <-- loop not working
- I'm a beginning at regular expressions, so the regex expression is broken
- The list of counties comes back as a single string, which regex is supposed to break apart
- County string example: "Barton, McPherson, Pawnee, Rice, Saline, Stafford"
- Needs to be list of strings: { "Barton", "McPherson", "Pawnee", "Rice", "Saline", "Stafford" }
- In US-County-Name.jmp...
- Uses a few nested loops to find rows where State = KS and other conditions
- When a row where State = KS, look at country, then finds which region it corresponds to by searching through the AA and then assigning the region (key) the county (value) is assigned to
- Saves the Region (key) to the row's Region column
names default to here(1);
clear log();
/* This block opens & readies US-County-Name.jmp */
countylist = open( "C:/Program Files/SAS/JMP/17/Maps/US-County-Name.jmp" );
countylist << New column( "County", formula( Word( 1, :Full Name Short, " " ) ) );
countylist << New column( "Region", Character );
countylist << New column( "State", formula( Word( -1, :Full Name Short, " " ) ) );
/* This block DOWNLOADS & opens the downloaded PDF & imports first page table */
request = New HTTP Request(
URL(
"https://www.kdhe.ks.gov/ArchiveCenter/ViewFile/Item/1246"
),
Method( "Get" )
);
file = request << Download( "$TEMP/Kansas Health Preparedness Regions.pdf", "replace", "show_progress" );
dt = Open( file,
PDF Tables(
Table(
table name(
"Kansas Health Preparedness Regions and Map Page(1) Table(1)"
),
add rows( page( 1 ), Rect( 0.4789, 1.0156, 10.5211, 7.8894 ) )
)
)
);
// For some reason, dt imports as a 1 item list of data tables
// Reassigns dt to a "Table" data type for further processing
dt = dt[1];
// Deletes empty rows in imported PDF table
dt << Select where( Is Missing( :Region ) ) << Delete Rows;
// Starting AA for data storage
region = Associative array();
// Stores region / county list data in AA
for( i = 1, i <= N Rows( dt ), i++,
templist = {};
temphold = dt:Counties Included[i];
tempitems = N Items( words( dt:Counties Included[i], "," ) );
show(tempitems);
// Breaks apart country string into individual counties and adds them to templist
for( n = 1, n <= tempitems, n++,
iteration = eval( eval expr( expr( eval insert( "\^n^" ) ) ) );
// Fix me! This is broken
insert into( templist, regex( "^\!"(\w*),*\!"$", iteration ) );
);
// Save the list as the value pair (county list, list of strings) to the respective key
region[ dt:region[i] ] = templist
);
// The region & county list is now stored in an AA!
//print( region );
/* This block will use data in AA to assign region to respective county in US-County-Name.jmp */
for( k = 1, k <= N rows( countylist ), k++,
// Condition where state = Kansas
try( if( countylist:State[k] == "KS",
// Iterates through AA keys (regions)
for( m = 1, m <= N Items( kskeys ), m++,
// Creates list of counties within m region
tempvals = region[m];
// Checks if Count present, then assigns region to dt
if( contains( tempvals, countylist:County[k] ), // County is in m list
countylist:Region[k] = kskeys[m]
);
);
) ); // try --> if county not found in PDF stated counties, no region will be assigned
);
// Closes out pdf table, if needed
close( dt, "No Save" );
Learning every day!