Initialize variables

Posted on 2011-05-03
Last Modified: 2013-11-16

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?

Question by:sonmic
    LVL 11

    Expert Comment


    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;

    Open in new window


    Author Comment


    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.
    LVL 11

    Accepted Solution


    Change the Proc sql part to:

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article describes some very basic things about SQL Server filegroups.
    CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now