1
votes

New SAS user.

I have a data set in which I'd like to initialize, say, 100 new variables VAR101, VAR102... VAR200. Here's one thing I tried:

data stuff;
    set a.stuff;
    do i=101 to 200;
        cat('var',put(i,3.)) = .;
    end;
run;

I get the following errors:

ERROR: Undeclared array referenced: cat.
ERROR: Variable cat has not been declared as an array.

I've tried to do this in several different ways. This was the one that made the most sense to me, but none of them have been fruitful so far. (I tried putting an "input" statement around the "cat" too.)

How can I do this?

(What I'm actually trying to do is much more complex, but I can't even figure this out.)

2
SAS variables are set to missing by default so there's no need to initialize a variable to missing. You may want to describe your more complex issue. - Reeza
I need to initialize a lot of variables at once, with specific naming conventions for the variables. The "variable range" thing won't work for me here, it's more like {a,b,c, or d}{#s 15-45 by 5s}{10-20}, 5-character codes, and they need to be in a specific order in the data set. I have a work around that is almost certainly not the best way to do this. (I'm generating a data set that consists of only one variable NEWVAR and then looping through that to add the new variables one-by-one.) - Schwa
Expand on what you need, you haven't provided enough information for us to offer solutions. I'm assuming your 'variables' are defined somewhere or have some sort of convention, even if it's in a data set. If it's in your head, there's no way around hard coding, in some manner. There are lots of way to 'add' variables but that's not really the way SAS thinks. - Reeza
OK, let's say I have a list of all the new variable names I want to initialize, a data set VARS with only one variable NEWVARS. I want to initialize all of these variables inside an existing data set OLDSET. I've tried many things... - Schwa
I keep trying to post code that I've tried, but apparently we can't use the code tags here in replies to comments. - Schwa

2 Answers

2
votes

You cannot use data step code to generate the name of variable to be used in the same data step. SAS needs to "compile" the code before it starts running the data step.

If you just want to define the variables then use a LENGTH or ATTRIB statement. For a range of variable names that all start with the same prefix and have a numeric suffix you can use a variable list like this.

data stuff;
  set a.stuff;
  length var101 - var200 8 ;
run;

If the variables are not already in your input dataset (a.stuff) then they will automatically be missing.

You should also look at the ARRAY statement if you want to apply a similar block of code to a list of variables.

data stuff;
  set a.stuff;
  by id;
  array newvar var101 - var200 ;
  if first.id then do i=1 to dim(newvar);
     newvar(i)=.;
  end;
  * other statements for your progam ;
run;
0
votes

Please check if this macro can resolve your query:

%macro abc;
%let i=101;

  %do %while (&i. <=201);

    data want&i.;
    var&i.=.;
    run;

    data want_final;
    merge want_final want&i.;
    run;

    proc datasets lib=work memtype=data;
    delete want&i.;
    run;
    quit;

    %let i=%eval(&i.+1);

%end;
%mend;

%abc;