BookmarkSubscribeRSS Feed
rboire
Calcite | Level 5

Given the output below after running some code in manipulating data from JSON.

data x;
infile '\\kashmir\BFD\sample.json.txt' lrecl=320000 dsd dlm='{},:[]';
input x : $400. @@;
run;
data want;
 merge x x(firstobs=2 rename=(x=value));
 if lowcase(x) in ("id" "time" "ip_address" "identity" "page"
"user_agent" "answer" "comment" "sessionid"
 "language" "locale" "channel");
run;
proc print data=want;
run;

 

Output:


                 Obs    x             value

                   1    id            89702492
                   2    time          2016-10-06 14:26:25 UTC
                   3    ip_address    111.111.111.111
                   4    identity      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   5    page          https://secure.hello.com
                   6    user_agent    Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit
                   7    answer        Other please say
                   8    comment       I dont know
                   9    SessionID     4444444444
                  10    Language      fr
                  11    Locale        Be
                  12    Channel       Affiliate
                  13    id            89702444
                  14    time          2014-10-14 14:26:25 UTC
                  15    ip_address    111.111.111.111
                  16    identity      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                  17    page          https://secure.goodbye.com
                  18    user_agent    Mozilla/5.0 (Windows NT 6.3; WOW64) ricjie
                  19    answer        Other please say
                  20    comment       I dont know
                  21    SessionID     3124444444
                  22    Language      fr
                  23    Locale        Be
                  24    Channel       nothing

 

 

How do I convert above data such that a new record begins with the key ID and all the subsequent keys along with ID are variables.

So in above case, have two records with variables beginning with ID and ending in channel. 

 

 

2 REPLIES 2
Reeza
Super User

See the methods illustrated here. 

How you read JSON is dependent on your SAS version, obviously the later versions are better. 

 

http://blogs.sas.com/content/sasdummy/2015/09/28/parse-json-from-sas/

art297
Opal | Level 21

I think you only need to transpose what you have. e.g.:

data want;
  set want;
  if x='id' then counter+1;
run;

proc transpose data=want out=want(drop=_: counter);
  by counter;
  var value;
  id x;
run;
  

Art, CEO, AnalystFinder.com

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 718 views
  • 0 likes
  • 3 in conversation