SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
keen_sas
Quartz | Level 8

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.

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

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.

jimbarbour
Meteorite | Level 14

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

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 666 views
  • 1 like
  • 3 in conversation