Qimeng Zhang
Qimeng Zhang

Reputation: 31

How to quote array variable in SAS

I have a dataset INPUT with a variable named color with couple of types:

obs  color
1     red
2     red
3     yellow
4     blue
...

Now I want to create flags for each types of the color:

DATA COLOR; SET INPUT;
ARRAY TYPES [3] RED YELLOW BLUE;
FOR I = 1 TO DIM(TYPES) DO; 
  IF COLOR = %unquote(%str(%')TYPES[I].%str(%')) THEN TYPE[I] = 1; 
  ELSE TYPE[I] = 0; 
END;
RUN;

So it not working well by using this way to added quotes for a array variable. Since I will need to create the same name of flag for each color type I thought I would be easier to write as Array. Please let me know if you know how to add quotes for array variables. So that I won't be need to write a many rows of IF STATEMENTS. When there are too many types of colors. Thanks.

Upvotes: 0

Views: 358

Answers (1)

Tom
Tom

Reputation: 51601

Arrays seem to be overkill for your example. If you want to create binary variables then assign the results of a boolean expressions to the variables.

data want;
  set have ;
  red = color='red';
  blue = color='blue';
  yellow = color='yellow';
run;

For your ARRAY idea to work you will need to use the VNAME() function to find the NAME of the variable that corresponds to the array element.

data want ;
  set have ;
  array types red blue yellow ;
  do i=1 to dim(types);
    types(i)=upcase(color)=upcase(vname(types(i)));
  end;
run;

Upvotes: 2

Related Questions