Best approach for parsing a query string and storing values during data step

Reply
Occasional Contributor
Posts: 10

Best approach for parsing a query string and storing values during data step

Hi,

I'm looking for a way to parsing a query string and storing values during data step. 

For example...

  • I have a query sting (from a logfile) in SAS table (tablename = someLogfile , column name = queryString):   &qp_ab=value1&qp_bc=value2&qp_ac=value3...&qp_i=valuei
  • I would to create a new table with a column for qp1, a column for qp2, qp3,...and a column for qpi .   the challenge that i'm having is that each queryString in the table someLogfile can be different.  for example the next query string read could be &qp_ab=value1&qp_ac=value3...&qp_x=valuex

So i don't know the number of query parameters, and the set of query parameters used can be different.   I'm thinking that I'll need to use an array and some macro but wondering if anyone has an easy way to do this and can give me some better ideas.  Thanks! John

data data_logfile;

  set someLogfile;

       queryString_2=translate(queryString,'&','?');

       count = countc(queryString_2,'&');

/*PARSE QUERY STING INTO AN ARRAY*/

/*WALK THROUGH THE QUERY STRING AND ASSIGN THE VALUE OF THE QUERY PARAMETER TO THE APPROPRIATE TABLE VARIABLE*/

        do while i<count;

               i+1;

       end;

run;

Super User
Posts: 11,101

Re: Best approach for parsing a query string and storing values during data step

I'm not quite sure what you expect but I think you might write that data out to a text file and then read it using NAMED input with a delimiter of &.

Though if your values have imbedded spaces this may not work but could be doable making sure there are two spaces before each variable name (i.e. replace the & with two spaces.

You really should provide at least a couple of lines of data for use to look at.

Occasional Contributor
Posts: 10

Re: Best approach for parsing a query string and storing values during data step

Thank you.   Here is an example of the data that we get.  We can retrieve it from the server its stored on, but we are limited right now in our ability to re-write the data into a text file.   We're exploring SAS as a option for parsing it into meaningful pieces.  There is other data in the file, but we are only interested in cs-uri-stem

example.png

Super User
Super User
Posts: 6,842

Re: Best approach for parsing a query string and storing values during data step

Better off storing this as name/value pairs.

data have;

  input querystring & $100. ;

cards4;

&qp_ab=value1&qp_bc=value2&qp_ac=value3...&qp_i=valuei

&qp_ab=value1&qp_ac=value3...&qp_x=valuex

;;;;

data want ;

set have;

* assign QUERYSTRING to NEXT to force SAS to define the length ;

next=querystring;

do i=1 by 1 until (next=' ');

     next=scan(querystring,i,'&','Q');

     name = scan(next,1,'=');

     value = substr(next,length(name)+2);

    if name ne ' ' then output;

end;

run;

proc print;

run;

Ask a Question
Discussion stats
  • 3 replies
  • 362 views
  • 0 likes
  • 3 in conversation