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?
There are several methods outlined here:
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?
Yes, that's correct!
For what purpose do you want to use these dummy variables?
Also, why are no variables created for 8_2 and 6_2?
It's just an example, but the code should create variables for each one
There are several methods outlined here:
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?
Thank you! This worked perfectly.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.