BookmarkSubscribeRSS Feed
BadMojoNixon
Calcite | Level 5

 

Hello everyone, I'm newish to SAS, so please bare with me. I'm trying to create multiple flag variables based on the contents of a parsed out variable. My macro isn't doing what I want it to do... it's parameter is passing as a static string, rather than a dynamic variable. Any help would be greatly appreciated.

-----------

Details!

This is my input data:

data Input;                                                                         
input Variable_source $15.;                          
datalines;                                                                          
01 03
01 03 04
01 05
01 02
;

Here is my Macro code

%macro parse (temp=);
	data output; set Input; 
		%local i;
		%do i = 1 %to %sysfunc(countw(&temp));
			%let code = %scan(&temp, &i, %str( ));
			VARIABLE_&code=1;
		%end;
	run;
%mend;

%parse (temp=Variable_source);

It gives me this:

Variable_sourceVARIABLE_variable_source
01 031
01 03 041
01 051
01 021

 

But I want this:

Variable_sourceVARIABLE_01VARIABLE_02VARIABLE_03VARIABLE_04VARIABLE_05
01 031 1  
01 03 041 11 
01 051   1
01 0211   

 

1 REPLY 1
CalleJ
Fluorite | Level 6

Hello!

 

The reason your macro doesn't work is because the macro is compiled before the data step is processed, i.e. the countw(&temp) will translate into countw(Variable_source) = 1 (same with the %scan()-function).

 

This is one way of doing it without using a macro:

 

data Input;                                                                         
input Variable_source $15.;                          
datalines;                                                                          
01 03
01 03 04
01 05
01 02
;

/* Looping through each dynamic array (using scan() to loop through the "words" and outputing each source as a new row) */
/* Creating dummy variable transpose_var=1 for transpose below and temporary variable rownumber to avoid sorting issues when transposing */
data work.input_spliced(drop=i);
	set work.input;
	rownumber = _n_;
	transpose_var = 1;
	do i = 1 to countw(Variable_source);
		Var_source_spliced = scan(Variable_source,i);
		output;
	end;
run;

/* Transposing the spliced table with variable prefix VARIABLE_ */
proc transpose data=work.input_spliced out=work.output(drop=rownumber _name_) prefix=VARIABLE_;
	by rownumber Variable_source;
	id Var_source_spliced;
	var transpose_var;
run;

 

Kind regards

/Calle

 

Edit: Realized there was some unnecessary code in there, so I trimmed the solution a bit. 🙂

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1 reply
  • 849 views
  • 0 likes
  • 2 in conversation