BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
valengvz
Fluorite | Level 6

Hi there, I'm working with a JSON file that looks like this:

 

{
   "parameters":{
      "type":"SEARCH_INDEX_LOADER",
      "filters":[
                        {
                                "type": "incrementalLoad",
                                "startTime": "2021-07-13T10:00:00.000Z"
                        }
      ]
   },
   "tasks":[
      {
         "parameters":{
            "type":"LOAD_DOCUMENT",
            "name":"EMPLEADO"
         }
      },      
      {
         "parameters":{
            "type":"LOAD_DOCUMENT",
            "name":"COMERCIO"
         }
      }

   ]
}

I'm looking for a way to change the "startTime": "2021-07-13T10:00:00.000Z", with the current date time, the only way that might work is to create the JSON file from scratch with PROC JSON procedure.

 

Does someone know if there is a way to do that inside SAS?

(I'm trying to do a reincremental reindexing for SAS visual investigator)

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Easy to read the data from the file. Then you can use the power of the INPUT and PUT statements.

data _null_;
  infile have dlm=':' dsd truncover ;
  file want;
  input name :$100. @ ;
  if name = 'startTime' then do;
    now = intnx('dtday',datetime(),0,'b');
    col = max(1,verify(_infile_,' '));
    put @col '"startTime": "' now E8601DT23.3 'Z"';
  end;
  else put _infile_;
run;

Results:

{
   "parameters":{
      "type":"SEARCH_INDEX_LOADER",
      "filters":[
                        {
                                "type": "incrementalLoad",
                                "startTime": "2021-07-23T00:00:00.000Z"
                        }
      ]
   },
   "tasks":[
      {
         "parameters":{
            "type":"LOAD_DOCUMENT",
            "name":"EMPLEADO"
         }
      },
      {
         "parameters":{
            "type":"LOAD_DOCUMENT",
            "name":"COMERCIO"
         }
      }

   ]
}

View solution in original post

5 REPLIES 5
jimbarbour
Meteorite | Level 14

Well, this isn't using any of SAS's JSON functions or procedures, but the quick and dirty way to do it is with a Data step and simply swap out the value, 1 for 1.  In the below code, I'm reading from and writing to a SAS dataset for ease of demonstration.  In reality, you'd want to read from and write to a text file.  Note that in the results that the original date-time, 13 July..., has been replaced with the current date-time of my SAS server, 23 July...

 

The Putlog statements are just there so it's easier to see what the code is doing; they're not essential to the final product of the program.

 

Code:

DATA	Have;
	INFILE	DATALINES	TRUNCOVER;
	INPUT
		Line_of_Data	$CHAR32767.;
DATALINES;
{
   "parameters":{
      "type":"SEARCH_INDEX_LOADER",
      "filters":[
                        {
                                "type": "incrementalLoad",
                                "startTime": "2021-07-13T10:00:00.000Z"
                        }
      ]
   },
   "tasks":[
      {
         "parameters":{
            "type":"LOAD_DOCUMENT",
            "name":"EMPLEADO"
         }
      },      
      {
         "parameters":{
            "type":"LOAD_DOCUMENT",
            "name":"COMERCIO"
         }
      }

   ]
}
;
RUN;

DATA	Want;
	KEEP	Line_of_Data;
	LENGTH	Time_Stamp	$128.;
	SET	Have;
	IF	INDEX(UPCASE(Line_of_Data), 'STARTTIME')	THEN
		DO;
			Start_Position	=	INDEX(UPCASE(Line_of_Data), 'STARTTIME') - 1;
			Time_Stamp		=	SCAN(Line_of_Data, 2, ' ');
			Stamp_Length	=	LENGTH(CAT('"startTime": ', Time_Stamp));
			PUTLOG	"NOTE:  Before "	Time_Stamp=;
			SAS_Time_Stamp	=	INPUT(COMPRESS(Time_Stamp,'"Zz'), ANYDTDTM20.);
			PUTLOG	"NOTE-  Before "	SAS_Time_Stamp=  SAS_Time_Stamp=	E8601DT23.3;
			PUTLOG	"NOTE-  ";
			SAS_Time_Stamp	=	DATETIME();
			PUTLOG	"NOTE-  After  "	SAS_Time_Stamp=  SAS_Time_Stamp=	E8601DT23.3;
			Time_Stamp		=	CATS('"', PUT(SAS_Time_Stamp, E8601DT23.3), 'Z"');
			PUTLOG	"NOTE-  After  "	Time_Stamp=;
			PUTLOG	"NOTE-  ";
			SUBSTR(Line_of_Data, Start_Position, Stamp_Length)	=	CAT('"startTime": ', Time_Stamp);
		END;
RUN;

 

Results:

jimbarbour_1-1627020293499.png

 

Jim

 

 

valengvz
Fluorite | Level 6
Thank you, I will start with this aproach.
jimbarbour
Meteorite | Level 14

Sounds good.  Please let me know if you encounter any problems, and let me know how it goes.

 

Good luck,

 

Jim

Tom
Super User Tom
Super User

Easy to read the data from the file. Then you can use the power of the INPUT and PUT statements.

data _null_;
  infile have dlm=':' dsd truncover ;
  file want;
  input name :$100. @ ;
  if name = 'startTime' then do;
    now = intnx('dtday',datetime(),0,'b');
    col = max(1,verify(_infile_,' '));
    put @col '"startTime": "' now E8601DT23.3 'Z"';
  end;
  else put _infile_;
run;

Results:

{
   "parameters":{
      "type":"SEARCH_INDEX_LOADER",
      "filters":[
                        {
                                "type": "incrementalLoad",
                                "startTime": "2021-07-23T00:00:00.000Z"
                        }
      ]
   },
   "tasks":[
      {
         "parameters":{
            "type":"LOAD_DOCUMENT",
            "name":"EMPLEADO"
         }
      },
      {
         "parameters":{
            "type":"LOAD_DOCUMENT",
            "name":"COMERCIO"
         }
      }

   ]
}
Vivi1997
SAS Employee

If I have my JSON in the following format:

{
   "parameters":{
      "type":"SEARCH_INDEX_LOADER",
      "filters":[
                        {
                                "type": "incrementalLoad",
                                "startTime": "2021-07-23T00:00:00.000Z"
                        }
      ]
   },
   "tasks":[
      {
         "parameters":{
            "type":"LOAD_DOCUMENT",
            "name":"EMPLEADO",
"filters":[ { "type": "incrementalLoad", "startTime": "2021-07-23T00:00:00.000Z" } ] }, "tasks":[ { "parameters":{ "type":"LOAD_DOCUMENT", "name":"EMPLEADO" } }, { "parameters":{ "type":"LOAD_DOCUMENT", "name":"COMERCIO" } } ] }

 And let say I want to edit the startTime under the second key i.e. tasks keyword instead of the first startTime under first parameters key. Then is it possible to do so?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 2091 views
  • 9 likes
  • 4 in conversation