<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic JSON engine: How to preserve long variable names and variable names with invalid characters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/JSON-engine-How-to-preserve-long-variable-names-and-variable/m-p/808441#M318781</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm reading a JSON file where some variable names are &amp;gt;32 characters, or have characters that are not allowed to be in a SAS variable name.&amp;nbsp; The JSON engine happily renames the variable names when it imports the data.&amp;nbsp; I'm wondering if there is any option to have the original variable name stored in the the SAS variable label automatically?&amp;nbsp; I think the EXCEL engine will do this, but I don't see a way to get the JSON engine to do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a workaround, my thought is to have the JSON engine create a map with the original names of the variables, and then read that map. Something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname=V7 ;

filename myjson temp ;
filename mymap temp ;

data _null_;
  file myjson ;
  put '[' ;
  put  '{"Variable Unallowed Characters ?.!":1}';
  put  ',' ;
  put  '{"Really Really Really Long Variable Name #1":2, "Really Really Really Long Variable Name #2": 3}';
  put  ',' ;
  put  '{"Really Really Really Long Variable Name #1":4}';
  put ']';
run;

libname myjson json fileref=myjson  map=mymap automap=create;
libname mymap  json fileref=mymap;


data vars ;
  set mymap.datasets_variables (keep=Name Path);
  if name="ordinal_root" then delete ;
  length Label $200 ;
  Label=scan(path,-1,'/') ;
  put (Name Label)(=) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which returns:&lt;/P&gt;
&lt;PRE&gt;21   data vars ;
22     set mymap.datasets_variables (keep=Name Path);
23     if name="ordinal_root" then delete ;
24     length Label $200 ;
25     Label=scan(path,-1,'/') ;
26     put (Name Label)(=) ;
27   run ;

NAME=Variable_Unallowed_Characters___ Label=Variable Unallowed Characters ?.!
NAME=Really_Really_Really_Long_Variab Label=Really Really Really Long Variable Name #1
NAME=Really_Really_Really_Long_Varia2 Label=Really Really Really Long Variable Name #2
NOTE: There were 4 observations read from the data set MYMAP.DATASETS_VARIABLES.
NOTE: The data set WORK.VARS has 3 observations and 3 variables.
&lt;/PRE&gt;
&lt;P&gt;So I think that could work, and I could macrocize it so that when I import a JSON file, I also grab the labels via above and then run PROC DATASETS to apply them to the output dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But wanted to ask if there was an easier way I'm overlooking.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;--Q.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Apr 2022 21:49:45 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2022-04-18T21:49:45Z</dc:date>
    <item>
      <title>JSON engine: How to preserve long variable names and variable names with invalid characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/JSON-engine-How-to-preserve-long-variable-names-and-variable/m-p/808441#M318781</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm reading a JSON file where some variable names are &amp;gt;32 characters, or have characters that are not allowed to be in a SAS variable name.&amp;nbsp; The JSON engine happily renames the variable names when it imports the data.&amp;nbsp; I'm wondering if there is any option to have the original variable name stored in the the SAS variable label automatically?&amp;nbsp; I think the EXCEL engine will do this, but I don't see a way to get the JSON engine to do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a workaround, my thought is to have the JSON engine create a map with the original names of the variables, and then read that map. Something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options validvarname=V7 ;

filename myjson temp ;
filename mymap temp ;

data _null_;
  file myjson ;
  put '[' ;
  put  '{"Variable Unallowed Characters ?.!":1}';
  put  ',' ;
  put  '{"Really Really Really Long Variable Name #1":2, "Really Really Really Long Variable Name #2": 3}';
  put  ',' ;
  put  '{"Really Really Really Long Variable Name #1":4}';
  put ']';
run;

libname myjson json fileref=myjson  map=mymap automap=create;
libname mymap  json fileref=mymap;


data vars ;
  set mymap.datasets_variables (keep=Name Path);
  if name="ordinal_root" then delete ;
  length Label $200 ;
  Label=scan(path,-1,'/') ;
  put (Name Label)(=) ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which returns:&lt;/P&gt;
&lt;PRE&gt;21   data vars ;
22     set mymap.datasets_variables (keep=Name Path);
23     if name="ordinal_root" then delete ;
24     length Label $200 ;
25     Label=scan(path,-1,'/') ;
26     put (Name Label)(=) ;
27   run ;

NAME=Variable_Unallowed_Characters___ Label=Variable Unallowed Characters ?.!
NAME=Really_Really_Really_Long_Variab Label=Really Really Really Long Variable Name #1
NAME=Really_Really_Really_Long_Varia2 Label=Really Really Really Long Variable Name #2
NOTE: There were 4 observations read from the data set MYMAP.DATASETS_VARIABLES.
NOTE: The data set WORK.VARS has 3 observations and 3 variables.
&lt;/PRE&gt;
&lt;P&gt;So I think that could work, and I could macrocize it so that when I import a JSON file, I also grab the labels via above and then run PROC DATASETS to apply them to the output dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But wanted to ask if there was an easier way I'm overlooking.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;--Q.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 21:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/JSON-engine-How-to-preserve-long-variable-names-and-variable/m-p/808441#M318781</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-04-18T21:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: JSON engine: How to preserve long variable names and variable names with invalid characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/JSON-engine-How-to-preserve-long-variable-names-and-variable/m-p/808468#M318793</link>
      <description>&lt;P&gt;I think you are on the right track.&lt;/P&gt;
&lt;P&gt;It could be as simple as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select cats(NAME,'=''',scan(PATH,-1,'/'),'''n') into :labels separated by ' ' 
  from MYMAP.DATASETS_VARIABLES 
  where NAME ^= "ordinal_root";
quit;            

data WANT;
  set MYJSON.ROOT; 
  label &amp;amp;labels.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Apr 2022 02:59:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/JSON-engine-How-to-preserve-long-variable-names-and-variable/m-p/808468#M318793</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2022-04-19T02:59:18Z</dc:date>
    </item>
  </channel>
</rss>

