Parse JSON object in SAS macro

前端 未结 3 1324
梦毁少年i
梦毁少年i 2020-12-06 23:53

Here is the input JSON file. It have to parse in SAS dataset.

\"results\":
[
 {
    \"acct_nbr\": 1234,
    \"firstName\": \"John\",
    \"lastName\": \"Smit         


        
3条回答
  •  北海茫月
    2020-12-07 00:38

    To answer your question with a SAS-only solution, your problems are twofold:

    • Use SCAN instead of substr to get the un-comma/quotationed portion
    • acct_nbr is a number, so you need to remove the final quotation mark from the input.

    Here's the correct code (I changed directories, you'll need to change them back):

    filename data 'c:\temp\json.txt';
    data testdata2;
        infile data lrecl = 32000 truncover scanover;
            input 
                @'"acct_nbr": ' acct_nbr $255.
                @'"streetAddress": "' streetAddress $255. 
                @'"city": "' city $255. 
                @'"state": "' state $2. 
                @'"postalCode": "' postalCode $255.;
    
            acct_nbr=scan(acct_nbr,1,',"');
            streetAddress = scan(streetAddress,1,',"');
            city = scan(city,1,',"');
            state = scan(state,1,',"');
            postalCode = scan(postalCode,1,',"');
    run;
    
    proc print data=testdata2;
    RUN;
    

提交回复
热议问题