BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Chelley
Calcite | Level 5

I need help creating new variables using macros. Below is my dataset.  The objective variables are character variables.  I need to run a macro that searches each objective variable and creates a corresponding variable.

 

eventobjective1objective2objective3
lecture1_64_27_8
lecture3_75_3 
small group8_26_2 

 

The macro would search each objective variable.  For example...

If Objective1= 1_6 then do;

Example1_6 = 1. 

end;

 

Desired Results

eventobjective1objective2objective3Example1_6Example4_2Example7_8Example3_7Example5_3
lecture1_64_27_8111  
lecture3_75_3    11
small group8_24_2  1   

 

 

Does anyone know how to create code for this?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

There are several methods outlined here:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-dummy-variables-Categorical-Var...

 

And depending on what proc you're using you may not need to do this if it supports CLASS statements.

 

For your data, here's one specific way to do it.

 

*Fake data;
data have;
infile cards dlm=',';
informat event $20.;
input event	(objective1-objective3) ($) ;
cards;
lecture,	1_6,	4_2,	7_8
lecture,	3_7,	5_3	, 
small group,	8_2,	6_2,	 
;;;;

*flip to a long format;
data have2;
set have;

array _ob(3) objective1-objective3;

row = _n_;
Value=1;

do i=1 to dim(_ob);
	if not missing(_ob(i)) then do;
		ID = vname(_ob(i));
		VarValue = _ob(i);
		output;
	end;
end;

drop objective1-objective3;
run;

*transpose to a wide format;
proc transpose data=have2 out=have3 prefix=Example;
by event row;
id varValue;
var Value;
run;

*change to 0/1;
proc stdize data=have3 out=have4 reponly missing=0;
run;

*merge back in original data;
data want;
merge have have4;
run;

@Chelley wrote:

I need help creating new variables using macros. Below is my dataset.  The objective variables are character variables.  I need to run a macro that searches each objective variable and creates a corresponding variable.

 

event objective1 objective2 objective3
lecture 1_6 4_2 7_8
lecture 3_7 5_3  
small group 8_2 6_2  

 

The macro would search each objective variable.  For example...

If Objective1= 1_6 then do;

Example1_6 = 1. 

end;

 

Desired Results

event objective1 objective2 objective3 Example1_6 Example4_2 Example7_8 Example3_7 Example5_3
lecture 1_6 4_2 7_8 1 1 1    
lecture 3_7 5_3         1 1
small group 8_2 4_2     1      

 

 

Does anyone know how to create code for this?


 

View solution in original post

7 REPLIES 7
Reeza
Super User
Looks like you're trying to create dummy variables. Does that sound correct?
Chelley
Calcite | Level 5

Yes, that's correct!

PeterClemmensen
Tourmaline | Level 20

For what purpose do you want to use these dummy variables?

PeterClemmensen
Tourmaline | Level 20

Also, why are no variables created for 8_2 and 6_2?

Chelley
Calcite | Level 5

It's just an example, but the code should create variables for each one

Reeza
Super User

There are several methods outlined here:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-dummy-variables-Categorical-Var...

 

And depending on what proc you're using you may not need to do this if it supports CLASS statements.

 

For your data, here's one specific way to do it.

 

*Fake data;
data have;
infile cards dlm=',';
informat event $20.;
input event	(objective1-objective3) ($) ;
cards;
lecture,	1_6,	4_2,	7_8
lecture,	3_7,	5_3	, 
small group,	8_2,	6_2,	 
;;;;

*flip to a long format;
data have2;
set have;

array _ob(3) objective1-objective3;

row = _n_;
Value=1;

do i=1 to dim(_ob);
	if not missing(_ob(i)) then do;
		ID = vname(_ob(i));
		VarValue = _ob(i);
		output;
	end;
end;

drop objective1-objective3;
run;

*transpose to a wide format;
proc transpose data=have2 out=have3 prefix=Example;
by event row;
id varValue;
var Value;
run;

*change to 0/1;
proc stdize data=have3 out=have4 reponly missing=0;
run;

*merge back in original data;
data want;
merge have have4;
run;

@Chelley wrote:

I need help creating new variables using macros. Below is my dataset.  The objective variables are character variables.  I need to run a macro that searches each objective variable and creates a corresponding variable.

 

event objective1 objective2 objective3
lecture 1_6 4_2 7_8
lecture 3_7 5_3  
small group 8_2 6_2  

 

The macro would search each objective variable.  For example...

If Objective1= 1_6 then do;

Example1_6 = 1. 

end;

 

Desired Results

event objective1 objective2 objective3 Example1_6 Example4_2 Example7_8 Example3_7 Example5_3
lecture 1_6 4_2 7_8 1 1 1    
lecture 3_7 5_3         1 1
small group 8_2 4_2     1      

 

 

Does anyone know how to create code for this?


 

Chelley
Calcite | Level 5

Thank you!  This worked perfectly.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 7 replies
  • 1844 views
  • 2 likes
  • 3 in conversation