sassas-studio

Sas create global variable based on cell value


Can anybody help me with the following: I have the a dataset that I filtered to only contain one row. I now want to create a global variable based on the value in a specific column.

I tried the following:

data _null_;

last_dat_value =  RWORK.dataset[1,column_name];

run;

the datasets looks like this:

date value
20220718 15

and I want the 15 in a global variable. Ive only been using SAS for two days, so apologies in advance


Solution

  • If you want to use it later you create it in a macro variable and reference it later with a &variableName

    Let's assume:

    *create fake data;
    data myDataset;
    date = mdy(7, 18, 2022);
    format date ddmmyyn8.;
    value = 15;
    output;
    run;
    
    data _null_; *_null_ means SAS will not create a data set as output;
    set myDataset; *indicate the input data set;
    call symputx('myValue', value, 'g'); *create the variable with a global scope;
    run;
    

    Then to use it later, say filter all people under the age of myValue.

    data below15;
    set sashelp.class;
    where age <= &myValue;
    run;
    

    Here is more references on Macro variables. https://stats.oarc.ucla.edu/sas/seminars/sas-macros-introduction/

    Generally speaking though, avoiding macros at the beginning is recommended. You should code without macros as much as possible. There are often other ways of doing things.