2

I'm trying to write a json file from a data step.

but my put statements always add unwanted spaces after variables.

put '           {"year":' year ',';

will create {"year":2013 ,

and

put '   {"name":"' %trim(name) '", ' ;

will create {"name":"Rubella virus ",

How can I remove the space after "Rubella virus" without overcomplicating things?


My best solution so far is to create a variable that uses cats and then put the newvariable a bit like this:

newvar=cats('{"name":"',name,'",');
put newvar;

Thanks!

stallingOne
  • 3,633
  • 3
  • 41
  • 63
  • try to use strip function instead of trim – andrey_sz Jul 11 '16 at 15:10
  • 1
    @andrey_sz this has nothing to do with the white space of the variable, but it is a property of the put function. It always insters a single white space after a variable. It has to be moved back by one space by using (+)-1. See my asnwer below. – Vasilij Nevlev Jul 11 '16 at 15:19

3 Answers3

4

You need to move pointer back by one step. You do this by asking to go forward by minus one step. Use this:

put ' {"name":"' name(+)-1 '", ' ;

Weird, I know, but it works.

Here is example with sashelp.class:

Code:

data _null_;
    set sashelp.class end = eof;

    if _N_ eq 1 then
        put '[';
    put '{ "Name":"' Name+(-1) 
        '","Sex":"' Sex+(-1) 
        '","Age":"' Age+(-1) 
        '","Height":"' Height+(-1) 
        '","Weight":"' Weight+(-1) 
        '"}';

    if eof then
        put ']';
    else put ',';
run;

Result:

    [
    { "Name":"Alfred","Sex":"M","Age":"14","Height":"69","Weight":"112.5"}
    ,
    { "Name":"Alice","Sex":"F","Age":"13","Height":"56.5","Weight":"84"}
    ,
    { "Name":"Barbara","Sex":"F","Age":"13","Height":"65.3","Weight":"98"}
    ,
    { "Name":"Carol","Sex":"F","Age":"14","Height":"62.8","Weight":"102.5"}
    ,
    { "Name":"Henry","Sex":"M","Age":"14","Height":"63.5","Weight":"102.5"}
    ,
    { "Name":"James","Sex":"M","Age":"12","Height":"57.3","Weight":"83"}
    ,
    { "Name":"Jane","Sex":"F","Age":"12","Height":"59.8","Weight":"84.5"}
    ,
    { "Name":"Janet","Sex":"F","Age":"15","Height":"62.5","Weight":"112.5"}
    ,
    { "Name":"Jeffrey","Sex":"M","Age":"13","Height":"62.5","Weight":"84"}
    ,
    { "Name":"John","Sex":"M","Age":"12","Height":"59","Weight":"99.5"}
    ,
    { "Name":"Joyce","Sex":"F","Age":"11","Height":"51.3","Weight":"50.5"}
    ,
    { "Name":"Judy","Sex":"F","Age":"14","Height":"64.3","Weight":"90"}
    ,
    { "Name":"Louise","Sex":"F","Age":"12","Height":"56.3","Weight":"77"}
    ,
    { "Name":"Mary","Sex":"F","Age":"15","Height":"66.5","Weight":"112"}
    ,
    { "Name":"Philip","Sex":"M","Age":"16","Height":"72","Weight":"150"}
    ,
    { "Name":"Robert","Sex":"M","Age":"12","Height":"64.8","Weight":"128"}
    ,
    { "Name":"Ronald","Sex":"M","Age":"15","Height":"67","Weight":"133"}
    ,
    { "Name":"Thomas","Sex":"M","Age":"11","Height":"57.5","Weight":"85"}
    ,
    { "Name":"William","Sex":"M","Age":"15","Height":"66.5","Weight":"112"}
    ]

Regards, Vasilij

Vasilij Nevlev
  • 1,449
  • 9
  • 22
  • I'm getting this error: `ERROR 22-322: Syntax error, expecting one of the following: a name, arrayname, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.` – stallingOne Jul 11 '16 at 15:24
  • @ooo I don't have a problem. I will expand my asnwer on sashelp.class example. May be that will help. – Vasilij Nevlev Jul 11 '16 at 15:50
2

For the character fields you can use the $QUOTE. format to add the quotes. Use the : to remove the trailing blanks in the value of the variable.

put '{ "Name":' Name :$quote.
    ',"Sex":' Sex :$quote.
    ',"Age":"' Age +(-1) '"' 
    ',"Height":"' Height +(-1) '"'
    ',"Weight":"' Weight +(-1) '"'
    '}'
;
Tom
  • 47,574
  • 2
  • 16
  • 29
1

If you are looking to have 'cleaner' code, you could build yourself a helper function or two using proc fcmp. This function will take a string description, the name of the field you want, and then whether or not to quote the returned string. Note that if your values can contain quotes, you may want to use the quote() function instead of t

Example Function:

proc fcmp outlib=work.funcs.funcs;

  function json(iName $, iField $, iQuote) $;    
    length result $200;
    quote_char = ifc(iQuote,'"','');
    result = cats('"', iName, '":',quote_char, iField, quote_char ); 
    return (result );
  endsub;

run;

Example Usage:

data _null_;
  set sashelp.class;
  x = catx(',',
           json("name",name,1), 
           json("age",age,0));
  put x;
run;

Example Output:

"name":"Alfred","age":14
"name":"Alice","age":13
"name":"Barbara","age":13
"name":"Carol","age":14
"name":"Henry","age":14
"name":"James","age":12
"name":"Jane","age":12
Robert Penridge
  • 8,424
  • 2
  • 34
  • 55