JSON modification

Reply
Contributor
Posts: 35

JSON modification

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

Super User
Posts: 1,230

Re: JSON modification

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;

 

Grand Advisor
Posts: 9,576

Re: JSON modification

Did you try PROC JSON ?
Ask a Question
Discussion stats
  • 2 replies
  • 198 views
  • 0 likes
  • 3 in conversation