- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data have ;
have="function=practical, param1=yes, param2=freason, param3=Event,join=usubjid"; output;
have="function=practical, param=1"; output;
have="function=practical, join=usubjid"; output ;
run ;
In the above example the specification have different parameters, out of which i have to identify only the parameters starting with param like param,param1,param2,param3 etc and to create the macro variables assigned to the respective variables.
For example in the first observation there are 3 paramenters which are having variables starting with param like param1,2,3 and macro variables will be created only for these 3 variables by considering the values assigned to them on right hand side after '=' symbol . Similarly in 2nd onservation only one variable is present , so only one macro variable associated wiht param will be created and in third observation since there is no variable starting with param, no macro variable will be created. Any suggestion how to separate them and create macro variables starting with param and the same macro variables will be used in subsequent steps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Like this?
data _null_;
set HAVE;
length STR $99;
do I=1 by 1 until (missing(STR));
STR=scan(HAVE,I,' ,');
if prxmatch('/\Aparam\d{0,2}=\w*\b/',STR) then
call execute('%put '||STR||';');
end;
run;
Replace %put with %let when you are satisfied the logic is what you want.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I suspect @ChrisNZ's logic will work quite well, but since I'm not quite as conversant with regular expressions, I went with a more conventional SAS programming approach. This isn't necessarily "better;" it's just another approach. "Take what you like and leave the rest" as it is said.
DATA _NULL_;
SET HAVE;
Token_Cnt = COUNTC(Have, ',') + 1;
DO i = 1 TO Token_Cnt;
Token = STRIP(SCAN(Have, i, ','));
Param_Idx = INDEX(UPCASE(Token), 'PARAM');
IF Param_Idx > 0 THEN
DO;
Equal_Idx = INDEX(SUBSTR(Token, Param_Idx), '=');
IF Equal_Idx > 0 THEN
DO;
Mac_Name = SUBSTR(Token, 1, (Equal_Idx - 1));
Mac_Val = SUBSTR(Token, (Equal_Idx + 1));
CALL SYMPUTX(Mac_Name, Mac_Val, 'G');
END;
END;
ELSE
END;
RUN;
From which I get the following results:
29 %PUT _USER_; GLOBAL PARAM 1 GLOBAL PARAM1 yes GLOBAL PARAM2 freason GLOBAL PARAM3 Event
Jim