BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tom
Super User Tom
Super User

The main issue is to simplify the macro so that it is just used to generate regular SAS statements. Your version was mixing macro logic and DATA step logic. First step is to just remove all of percent signs from the middle of your macro definition.

I will try to convert your example code using your variable names (if I can get copy and paste to work in this mini editor).

%macro FindParseAfter
(VarName            /* String to Parse */
,WhatItsLookingFor  /* Separator characters */
,TargetVarName      /* Variable to receive value */
,TempVar=_temp      /* temporary variable to hold substring value */
,LengthVar=_length  /* Temporary variable to hold length */
);
 
if find(&VarName,&WhatItsLookingFor) then do;
  &LengthVar = find(&VarName, &WhatItsLookingFor)+lengthc(&WhatItsLookingFor);
  &TempVar= substr(&VarName,&LengthVar,300);
  if find(&TempVar, '&') then do;
    &TargetVarName = scan(&TempVar,'&');
  end;
  else if find(&TempVar, '?') then do;
    &TargetVarName= scan(&TempVar,'?');
  end;
  else if find(&TempVar, '#') then do;
    &TargetVarName = scan(&TempVar,'#');
  end;
  else if find(&TempVar, '/') then do;
    &TargetVarName = scan(&TempVar,'/');
  end;
  else &TargetVarName = &TempVar;
end;
 

%mend;

Now your desired program becomes.

Data Step2;
  set Step1;
  %FindParse(URL, 'keyword',Keyword);
  %FindParse(URL, 'sortOption', Sort );
  %FindParse(URL, 'autoRedirect', AutoRedirect );
  %FindParse(URL, 'sid', SID);
  %FindParse(URL, 'psid', PSID);
run;

Now that you have a macro that is structured in a usable way you can work on the internal part about HOW to actually parse the strings.  That to me is really a different question than the one you posted to start.

Doxastic
Calcite | Level 5

THANKS TOM!  From that I was able to get something working, and much of what I want is in it.

%macro FindParseAfter

(Where2Start, OutVarName, VarName=URL, Number2Keep=1, BegParse=_temp, LenVar=_length);

if find(&VarName,&Where2Start) then do;

  &LenVar = find(&VarName, &Where2Start)+lengthc(&Where2Start);

  &BegParse = substr(&VarName,&LenVar,360);

  if find(&BegParse, '&') then do;

    &OutVarName = scan(&BegParse,&Number2Keep,'&');

  end;

  else if find(&BegParse, '?') then do;

    &OutVarName = scan(&BegParse,&Number2Keep,'?');

  end;

  else if find(&BegParse, '#') then do;

    &OutVarName = scan(&BegParse,&Number2Keep,'#');

  end;

  else if find(&BegParse, '/') then do;

    &OutVarName = scan(&BegParse,&Number2Keep,'/');

  end;

  else &OutVarName = &BegParse;

end;

drop _temp _length

%mend;

It would be nice if there were only 2 lines of parsing code, and I used some kind of END macro variable.  That way if the ending symbols changed to works, or different symbols, or more symbols.... it would only be a matter of changing the EndChar macro variable definition.  But that's ok.  This is useable code and I can start learning regular expression, then go from there.

Thanks again to everyone!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 16 replies
  • 5913 views
  • 6 likes
  • 7 in conversation