Dynamically create variables in the data step based on values of another variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Dynamically create variables in the data step based on values of another variable

I regularly have to process flags from comma-separated character fields that look like this:

example_field = "AX,EQ,XX,TY,M,GY,TS,Z,WP,DL";

My current process involves the following steps:

  1. Data step that iterates through the comma-separated values and outputs each one into a new dataset
  2. Proc freq of those values and store them into macro variables
  3. A second data step that checks for the presence of the values and assigns them to a binary variable flag

I am wondering whether all of this can be reduced to a single datastep.  I have dozens of variable like this and I would like to create a macro that can dynamically create flags for each comma-separated value in one datastep.  Something like this:

data tmp;   

     example_field = "AX,EQ,XX,TY,M,GY,TS,Z,WP,DL";   

     %expand_list(example_field);

   

/* yields the output:

     flag_AX

     flag_EQ

     flag_XX

     ...

     flag_DL

*/

run;

Is this wishful thinking?  I understand the timing issues between the data step and the macro compilation steps so I think I may be out of luck.  I'm hoping someone can give me a glimmer of hope, though.  Maybe something like proc FCMP?  The flag doesn't have to be strictly [1,0], it could also be [1, .]


Accepted Solutions
Solution
‎08-21-2013 03:34 PM
Super User
Posts: 19,770

Re: Dynamically create variables in the data step based on values of another variable

Bad example data so hard to determine if this is correct or not:

data have;

example_field = "AX,EQ,XX,TY,M,GY,TS,Z,WP,DL";

output;

run;

data want1;

    set have;

    i=1;

    do while (scan(example_field, i, ",") ne "");

    var_flip=scan(example_field, i, ",");

    value=1;

    output;

    i+1;

    end;

run;

proc transpose data=want1 out=want2 prefix=flag_;

id var_flip;

var value;

run;

View solution in original post


All Replies
Super User
Posts: 19,770

Re: Dynamically create variables in the data step based on values of another variable

Post sample data and sample output I think to help understand the problem.

I think you could at least combine step 2/3 using a proc transpose step instead of macro variables, but would need to see more info to be sure.

New Contributor
Posts: 3

Re: Dynamically create variables in the data step based on values of another variable

Have you looked at the SYMPUT and SYMGET routines to see if they can help you accomplish what you are after.  See here for documentation: SAS(R) 9.2 Macro Language: Reference

Occasional Contributor
Posts: 17

Re: Dynamically create variables in the data step based on values of another variable

Posted in reply to scottdmurff

Yes, thanks, I've looked into those.  The problem I'm having is that the variables I want to create derive their names from the values of another field.  SYMGET will assign a macro variable value to a variable I have already created, but it won't create the variable for me dynamically.

Solution
‎08-21-2013 03:34 PM
Super User
Posts: 19,770

Re: Dynamically create variables in the data step based on values of another variable

Bad example data so hard to determine if this is correct or not:

data have;

example_field = "AX,EQ,XX,TY,M,GY,TS,Z,WP,DL";

output;

run;

data want1;

    set have;

    i=1;

    do while (scan(example_field, i, ",") ne "");

    var_flip=scan(example_field, i, ",");

    value=1;

    output;

    i+1;

    end;

run;

proc transpose data=want1 out=want2 prefix=flag_;

id var_flip;

var value;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 264 views
  • 0 likes
  • 3 in conversation