BookmarkSubscribeRSS Feed
jgautreau
Fluorite | Level 6

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;

3 REPLIES 3
ballardw
Super User

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.

jgautreau
Fluorite | Level 6

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

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 1276 views
  • 0 likes
  • 3 in conversation