0

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

gumpy007
  • 65
  • 5

2 Answers2

2

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

Let's assume:

  • data set name is myDataset
  • variable name is value
  • Single observation/row in the data set
  • Macro variable output name is myValue
*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.

Reeza
  • 20,510
  • 4
  • 21
  • 38
0

SAS does not have any "global variables". All variables live in datasets. If you want to use a value from the first observation of one dataset in a data step that is using some other dataset as its main input then just read it in once at the start of the data step and its value will stay the same for the whole data step.

To change the variable's name from column_name to last_dat_value you can use the RENAME= dataset option.

data want;
  if _n_=1 then 
    set RWORK.dataset(keep=column_name rename=(column_name=last_dat_value) obs=1)
  ;
  set have;
  days_till_last = last_dat_value - date ;
run;
Tom
  • 47,574
  • 2
  • 16
  • 29