BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mathias
Quartz | Level 8

Hello,

 

Has anyone worked with firebase yet? (www.firebase.com)

 

I need to push data from an automated SAS egp to firebase.

This will allow me to request it easily from a web app and set user permissions on it.

 

https://www.firebase.com/docs/rest/guide/saving-data.html

 

Beause it's still new, I cannot find a single article/post/question about this.

 

Many thanks,

 

EDIT: I have SAS 9.3 and I'm working in Enterprise Guide 5.1 on Windows7

1 ACCEPTED SOLUTION

Accepted Solutions
mathias
Quartz | Level 8

This is working for me:

 

 

%let firebaseURL = https://YOURFIREBASEAPP.firebaseio.com/;
%let filePath = \\FILESERVER\BLA\BLA\BLA\;
%let jsonFile = MYDATA.json;

filename resp TEMP;
filename headout TEMP;
filename input "&filePath.&jsonFile";

proc http 
	method="PUT"
	url="&firebaseURL.&jsonFile"
	in=input
	out=resp
	headerout=headout
;run;

 

(POST is also working but adds a random id to your data.)

 

View solution in original post

7 REPLIES 7
BrunoMueller
SAS Super FREQ

I have no experience with this. But since firebase supports a REST Interface you might be able to use Proc HTTP

AhmedAl_Attar
Rhodochrosite | Level 12

Mathias,

 

I would look into the following two Procedures in SAS 9.4

 - Proc JSON: To convert your SAS tables into JSON format

 - Proc HTTP: To connect to your Firebase database via Rest

 

Good luck,

Ahmed

mathias
Quartz | Level 8

I forgot to say that I'm working on SAS 9.3 and with Enterprise guide 5.1

I don't think I have proc json, but I already have created the json file with a data step.

mathias
Quartz | Level 8

It worked!

 

I'll post the code here when I'm back at work Tuesday.

mathias
Quartz | Level 8

This is working for me:

 

 

%let firebaseURL = https://YOURFIREBASEAPP.firebaseio.com/;
%let filePath = \\FILESERVER\BLA\BLA\BLA\;
%let jsonFile = MYDATA.json;

filename resp TEMP;
filename headout TEMP;
filename input "&filePath.&jsonFile";

proc http 
	method="PUT"
	url="&firebaseURL.&jsonFile"
	in=input
	out=resp
	headerout=headout
;run;

 

(POST is also working but adds a random id to your data.)

 

FriedEgg
SAS Employee

I created a JAVA adapter for Firebase.  Check it out!

 

https://github.com/FriedEgg/firebase4sas

 

filename fbase "/path/to/firebase-client-jvm-2.3.1.jar"; /*https://cdn.firebase.com/java/firebase-client-android-2.3.1.jar*/
filename fb4sas "/path/to/firebase4sas.jar"; /*https://github.com/FriedEgg/firebase4sas/releases*/

/* create a file reference for the classes we are going to compile below with PROC GROOVY*/
filename cp temp;
proc groovy classpath=cp;

   /* add the two dependent libraries*/
   add classpath=fbase;
   add classpath=fb4sas;

   /* create a Model for the objects we are going to load. */
   submit load;
      public class Person {
         private String sex;
         private double age;
         private double height;
         private double weight;

         public Person(String sex, double age, double height, double weight) {
            this.sex    = sex;
            this.age    = age;
            this.height = height;
            this.weight = weight;
         }

         public String getSex() {
            return sex;
         }

         public double getAge() {
            return age;
         }

         public double getHeight() {
            return height;
         }

         public double getWeight() {
            return weight;
         }

         public String toString() {
            return "Person: Sex=" + sex + ", Age=" + age + ", Height" + height + ", Weight" + weight;
         }
      }
   endsubmit;

   /* create a class from the abstract to perform the load of our model */
   submit load;
      import java.lang.InterruptedException;

      public class PersonLoader extends AbstractFirebase4Sas {
         public PersonLoader(String firebase, String dsn, String name, String sex, double age, double height, double weight) {
            setFirebase(firebase);

            Person p = new Person(sex, age, height, weight);
            setLoaderObject(p);

            addChild(dsn);
            addChild(name);

            try {
               loadObject();
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
         }
      }
   endsubmit;

   /* you can test it in PROC GROOVY */
   /*
   submit;
      lp = new LoadPerson("MYFIREBASE", "class", "Frog", "F", 14.1, 15.1, 16.1);
   endsubmit;
   */

quit;

/* add compiled classes from PROC GROOVY to session CLASSPATH */
options set=classpath "%sysfunc(pathname(cp,f))";

/* use data step JavaObject Component to load our data set to Firebase */
data _null_;
   set sashelp.class indsname=dsn;

   declare javaObj lp;
   lp = _new_ javaObj ("LoadPerson","MYFIREBASE",scan(dsn,2,'.'),name,sex,age,height,weight);

   lp.flushJavaOutput();
   lp.delete();

run;

 

You can create you own model and loader classes simply enough to load any table.  It should work with SAS 9.3+ 

mathias
Quartz | Level 8

I've made a macro that takes a sas dataset and writes it to firebase with parametrable variable names

https://gist.github.com/mathiasleroy/f22face83d696b45c17a

 

%macro pushFirebase(dataset,firebaseName,fbObject,variables);
/*
	! change temporaryFilePath to fit your environement

	dataset 	= sas data set to push to firebase
	firebaseName= name of your firebase app name (xxx.firebaseio.com)
	fbObject  	= name of the firebase object to create
	variables 	= list of variables to include, separated by dashes -
*/


	/* CONSTANTS */
	%let temporaryFilePath = \\sas9xbi\Data2\tmp\;

	/* VARIABLES */
	%let firebaseURL = https://&firebaseName..firebaseio.com/;
	%let fileName = &fbObject..json;
	%let variablesLength =  %sysfunc(count(&variables,-));

	
	/********************************************************************
		CREATE JSON OBJECT
	********************************************************************/

	data _null_;
		set &dataset nobs=nobs end=end;
		file "&temporaryFilePath.&fileName";

		if _n_=1 then put '{';
		
			put '"' %scan(&variables,1,-) +(-1) '":{';

				%do i = 1 %to &variablesLength+1;
					variable = %sysfunc(strip("%scan(&variables,&i,-)"));
					*this is a repetition, but it makes json cleaner;
					if (&i ne 1) then put ',"' variable +(-1) '":' %scan(&variables,&i,-) ;
					else put '"' variable +(-1) '":' %scan(&variables,&i,-) ;
				%end;
			put '}';

			if not end then do;
				put ',';
				end;

		if end then do;
			put '}';
			end;
		run;



	/********************************************************************
		PUSH TO FIREBASE 
	********************************************************************/

	filename resp TEMP;
	filename headout TEMP;
	filename input "&temporaryFilePath.&fileName";

	proc http 
		method="PUT"
		url="&firebaseURL.&fileName"
		in=input
		out=resp
		headerout=headout
		;run;

%mend;

%pushFirebase(xxx.xxx,xxx,xxx,xxx-xxx-xxx-xxx);

  

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 2663 views
  • 3 likes
  • 4 in conversation