Hi,
In a website I am reading data from a certain location in SAS using code similar to:
const response = await fetch('/JAVASCRIPT/MaxSal/JobCodes.json');
const data = await response.json();
The values from the dataset are displayed in a website as an html table.
After the user modifies the table I am creating a JSON string. I am trying to post the values back to the same location however this is not working.
const response = fetch('/JAVASCRIPT/MaxSal/JobCodesNew.JSON', {;
method: 'POST',
headers: {;
Content-Type':'application/json','
Accept': 'application/json'
body: JSON.stringify
}
Please don't spend time reading the code I am quite sure the syntax is correct. The question is can I post back to the same location that I read the original file from? I have tried using different methods and it didn't work and there is no error.
Thanks Magued
Hi Tom,
Thanks for the reply and you are correct. You have to call a SAS stored procedure you can't just write the data.
Here is the code I used and it works doesn't mean it is the best code.:
Javascript:
function submitTableData() { var table = document.getElementById("dataTable"); var data = []; // Loop through table rows (skip header row 0) for (var i = 1; i < table.rows.length; i++) { var row = table.rows[i]; var rowData = { name: row.cells[0].innerText, age: parseInt(row.cells[1].innerText) }; data.push(rowData); } var jsonData = JSON.stringify(data); var sasEndpoint = "http://your-sas-server/SASJobExecution/?_program=/Folder/YourSASProgram&_action=execute"; // Replace with your actual endpoint // For large JSON data, consider using the Blob approach as described in SAS documentation var formData = new FormData(); formData.append("myjson", jsonData); // 'myjson' is the input parameter name the SAS program expects // Send the data using Fetch API fetch(sasEndpoint, { method: 'POST', body: formData // Use FormData to send parameters including JSON string }) .then(response => response.text()) .then(result => { document.getElementById("responseArea").innerHTML = "Response from SAS: " + result; console.log('Success:', result); }) .catch(error => { document.getElementById("responseArea").innerHTML = "Error: " + error; console.error('Error:', error); }); }
SAS Server Program (Server-Side)
%global MYJSON;
/* Copy the JSON data from input parameter to a temporary file */
filename indata temp;
data _null_;
file indata;
/* Use resolve(symget()) to handle potential length issues if data is very long */
length str $32767;
str = resolve(symget('MYJSON'));
put str;
run;
/* Use the JSON engine to read the data from the temporary file into a SAS dataset */
libname indata json;
proc copy inlib=indata outlib=work;
select alldata; /* 'alldata' is a common default table created by the JSON engine */
run;
libname indata clear; /* Clear the libname */
/* Process the data in the work.alldata dataset as needed */
proc print data=work.alldata;
title 'Data Received from HTML Table';
run;
/* You can also write a response back to the HTML page */
data _null_;
file _webout;
put 'Data successfully received and processed.';
run;This is one of the cases where it may be a good idea to include all of the code involved.
For example, do you close the connection completely after after reading the data? If not then there might be a locking issue.
Do you or your organization control the website? It is quite likely that displayed html is generated from another source for display as html and that source may well be what needs to be changed. If your organization controls the site then you may be able to work with the site developers/maintenance staff to determine what access and files are needed.
Thanks for the reply,
It is an internal website so we have control over it. The webpage is treated like an excel file where we change the values of some cells and we want to create a new dataset with the new values.
I can't include all the code. We are using DATA step to generate the html table. To populate a dropdown I am using await fetch('/JAVASCRIPT/MaxSal/JobCodes.json') this is the folder where I get the json file from.
I was expecting I would be able to save the new dataset in the same location using similar code.
Regards Magued
You're syntax is JavaScript but is your question about SAS programming? Is this in the context of a SAS job with an HTML front end?
Hi Chris,
Regarding your question: Is this in the context of a SAS job with an HTML front end? the answer is Yes.
My question is why I can get "get" data from a certain location and not "Post" it in the same location using Json. Please note that in debug I am not getting any errors when I send the data. It could be a Read/Write permission on the SAS server.
Magued
I don't understand why you would think that the same URL could be used for both POST and FETCH commands?
Do you have documentation on the interface for the URL you are connecting to?
Hi Tom,
Thanks for the reply and you are correct. You have to call a SAS stored procedure you can't just write the data.
Here is the code I used and it works doesn't mean it is the best code.:
Javascript:
function submitTableData() { var table = document.getElementById("dataTable"); var data = []; // Loop through table rows (skip header row 0) for (var i = 1; i < table.rows.length; i++) { var row = table.rows[i]; var rowData = { name: row.cells[0].innerText, age: parseInt(row.cells[1].innerText) }; data.push(rowData); } var jsonData = JSON.stringify(data); var sasEndpoint = "http://your-sas-server/SASJobExecution/?_program=/Folder/YourSASProgram&_action=execute"; // Replace with your actual endpoint // For large JSON data, consider using the Blob approach as described in SAS documentation var formData = new FormData(); formData.append("myjson", jsonData); // 'myjson' is the input parameter name the SAS program expects // Send the data using Fetch API fetch(sasEndpoint, { method: 'POST', body: formData // Use FormData to send parameters including JSON string }) .then(response => response.text()) .then(result => { document.getElementById("responseArea").innerHTML = "Response from SAS: " + result; console.log('Success:', result); }) .catch(error => { document.getElementById("responseArea").innerHTML = "Error: " + error; console.error('Error:', error); }); }
SAS Server Program (Server-Side)
%global MYJSON;
/* Copy the JSON data from input parameter to a temporary file */
filename indata temp;
data _null_;
file indata;
/* Use resolve(symget()) to handle potential length issues if data is very long */
length str $32767;
str = resolve(symget('MYJSON'));
put str;
run;
/* Use the JSON engine to read the data from the temporary file into a SAS dataset */
libname indata json;
proc copy inlib=indata outlib=work;
select alldata; /* 'alldata' is a common default table created by the JSON engine */
run;
libname indata clear; /* Clear the libname */
/* Process the data in the work.alldata dataset as needed */
proc print data=work.alldata;
title 'Data Received from HTML Table';
run;
/* You can also write a response back to the HTML page */
data _null_;
file _webout;
put 'Data successfully received and processed.';
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.