BookmarkSubscribeRSS Feed
samira
Obsidian | Level 7

Hello, 

 

I have the following table which is result of proc transpose: 

 

Untitled.png

 

I need a json structure like below:

 

 

{

"week": [1,2,3,.....53]

''year2012": [...entire column....]

"avg": [...entire column....]

"yhat": [...entire column....]

"sd": [...entire column....]

"ucl": [...entire column....]

"lcl": [...entire column....]

}

 

I have gotten the folloing code for creating my desire json structure: 

 

data _null_;
  file _webout;
  set mydata end=_last;
  array xcol{*} col:;

  if _n_ = 1 then do;
    put "{";
  end;

  put _name_ $quote. ":[" @;

  do i = 1 to dim(xcol);
    if i < dim(xcol) then do;
      put xcol{i} "," @;
    end;
    else do;
      put xcol{i}  @;
    end;
  end;

  if _last = 0 then do;
    put "],";
  end;
  else do;
    put "]";
  end;

  if _last = 1 then do;
    put "}";
  end;
run; 

 but when I ran above code I got the following result: 

 

 

{
"week" :[],
"year2012":[],
"avg" :[],
"yhat" :[],
"sd" :[],
"ucl" :[],
"lcl" :[]
}

 

I am not sure why it does not have value?!!

 

I am new in SAS, I would really appriciated if someone help me.

 

Thanks,

Samira

2 REPLIES 2
Shmuel
Garnet | Level 18

Your havn't posted your log. Look for NOTEs or Warnings in it.

 

Assuming your variable names are: WEEK: , AVG: , YHAT:  etc.

you should define arrays for each of them:

 

data _null_;
  file _webout;
  set mydata end=_last;
  /*  array xcol{*} col:; <<<<< are there variables with prefix COL ? ****/
array weekn{*} week:;
array yhatn{*} yhat:;
array agen{*} age:;
.... array per each variables prefix ...
.... your code, replacing the COL loop and dupliacte it per each array ...
run;

 

Ksharp
Super User
Did you try PROC JSON ?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 2 replies
  • 549 views
  • 0 likes
  • 3 in conversation