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 ?

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1018 views
  • 0 likes
  • 3 in conversation