BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sateh
Fluorite | Level 6

I have a table in sas (see image) where I need to loop through each record to generate a json for each record (product).

sateh_3-1668045639132.png

 

each json generated must have the name of the product.

my code to generate my json is this:

sateh_2-1668045523181.png

I really don't know how I should do this. 😞

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Post your data and code as text, retyping it is a pain in the ...;

 

Basically you can turn your small proc json into a macro and call it once for each line using CALL EXECUTE.

https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

 

%macro create_json(producto=);
proc json out="/home/folder_json/producto_&producto..json" pretty nosastags;
export public.productos(where=id_producto=&producto).);
quit;

%mend;

*call macro for each line;
data _null_;
set public.productos;
str = catt('%create_json(producto=', id_producto, ');');
call execute(str);
run;

@sateh wrote:

I have a table in sas (see image) where I need to loop through each record to generate a json for each record (product).

sateh_3-1668045639132.png

 

each json generated must have the name of the product.

my code to generate my json is this:

sateh_2-1668045523181.png

I really don't know how I should do this. 😞

 


 

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

I am not sure what you mean by "a json".

If you want to generate a JSON file that has the information the dataset you took the selfie of then the code you posted the second selfie of looks reasonable.

How is the resulting file different than what you wanted?

 

Note: If you want coding help post the data as DATA and not as PICTURES.  Also explain the output you want more clearly.  Proving the desired output for the example input data you share will go long way to help clarify your description of the desired results.

sateh
Fluorite | Level 6

Yes, exactly what I need is a JSON file for each product, There should be 8 JSON files left because there are 8 products (records) in the table.
the result should be for example:
product_8085.json
product_8076.json
product_8087.json
product_8025.json
product_8060.json
product_8745.json
product_9528.json
product_6058.json
attached image of my data

sateh_0-1668087004839.png

 

andreas_lds
Jade | Level 19

and what will be the content of the json files?

Tom
Super User Tom
Super User

If you want to generate a lot of text files from a dataset (a JSON file is just a text file) then use a data step.  You can use the FILEVAR= option of the FILE statement to name a variable that will contain the name of the file to be written.

 

So the general structure of your program could be:

data _null_;
  set myfile;
  length filename $200 ;
  filename = cats('product_',product,'.json');
  file json filevar=filename;
*   code here to write the json text using PUT statement ;
run;

If your dataset only has the 3 or four variables you show in your photograph then the PUT statement to write JSON text will be trivial.  But you need to show an example of the JSON format you actually want to make.  And explain what parts of it need to come from the values of the variables in the dataset.  If the MYFILE dataset has multiple observations for the same product then you will probably need to make sure it is sorted and then you can use BY group processing in the dataset.

sateh
Fluorite | Level 6

This is an example of how a JSON file of a single product should look like, in this case the first product 8085

 

sateh_0-1668089704659.png

 

Reeza
Super User

Post your data and code as text, retyping it is a pain in the ...;

 

Basically you can turn your small proc json into a macro and call it once for each line using CALL EXECUTE.

https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

 

%macro create_json(producto=);
proc json out="/home/folder_json/producto_&producto..json" pretty nosastags;
export public.productos(where=id_producto=&producto).);
quit;

%mend;

*call macro for each line;
data _null_;
set public.productos;
str = catt('%create_json(producto=', id_producto, ');');
call execute(str);
run;

@sateh wrote:

I have a table in sas (see image) where I need to loop through each record to generate a json for each record (product).

sateh_3-1668045639132.png

 

each json generated must have the name of the product.

my code to generate my json is this:

sateh_2-1668045523181.png

I really don't know how I should do this. 😞

 


 

sateh
Fluorite | Level 6
Thank you, this approach really helped me a lot.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 786 views
  • 0 likes
  • 4 in conversation