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

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, .]

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

4 REPLIES 4
Reeza
Super User

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.

scottdmurff
Calcite | Level 5

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

Zelazny7
Fluorite | Level 6

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.

Reeza
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 4 replies
  • 1590 views
  • 0 likes
  • 3 in conversation