Link to home
Start Free TrialLog in
Avatar of sonmic
sonmic

asked on

Initialize variables

Hi,

I have a SAS-dataset with 53 variables. When a certain variable is different from zero then i want to set 39 variables (variable 4 to 42) to zero. I can set them one by one to zero, but is there another way?

Tx
sonmic
Avatar of theartfuldazzler
theartfuldazzler
Flag of South Africa image

Hi

The best way (I think) is to use an ARRAY statement.

The issue is how to list the variable names in the ARRAY statement.  If the variables follow a certain numbering format (say VAR1  VAR2  VAR3 ... VAR53) then the statement can be as follows:

ARRAY ArrName (*) VAR1 - VAR53;

If the variable's are in  the correct order in the dataset, then you can use the following code to get a list of variable names into the macro variable &var_names:

proc sql noprint;
  select name into : var_names separated by ' '
   from dictionary.columns where libname = 'WORK' and memname = 'TEST';
%put &var_names;

Note that WORK and TEST must be in CAPITALS.  (TEST is the name of the dataset)


To answer your query, try the following full code:


 
proc sql noprint;
  select name into : var_names separated by ' '
   from dictionary.columns where libname = 'WORK' and memname = 'TEST';

%put &var_names;

DATA Test;
  set test;
    array ArrName (*) &var_names;

IF Certain_variable ~= 0 then do i = 4 to 53;
   ArrName[i] = 0;
end;
RUN;

Open in new window

Avatar of sonmic
sonmic

ASKER

Hi,

Tx for your answer but i get the message :
ERROR: All variables in array list must be the same type, i.e., all numeric or character.
ASKER CERTIFIED SOLUTION
Avatar of theartfuldazzler
theartfuldazzler
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial