Discussion:
How to specify a var is null in SAS?
(too old to reply)
palapara
2011-01-11 15:15:09 UTC
Permalink
Hi there,

I think I have messed up the programming languages..

How to specify a var is null in SAS?

Say I want to have a command:

if id is null then id=lag(id);

is the above correct or should be something such as missing, etc...?

Thanks!
Reeza
2011-01-11 16:58:33 UTC
Permalink
Post by palapara
Hi there,
I think I have messed up the programming languages..
How to specify a var is null in SAS?
if id is null then id=lag(id);
is the above correct or should be something such as missing, etc...?
Thanks!
You probably want something like this:

temp=lag(id);
if missing(id) then id=temp;

Lag isn't the value in the previous row, it messes with my head
sometimes, but the above will work.
palapara
2011-01-11 18:49:34 UTC
Permalink
thanks Reeza.

Somehow, it doesn't work as I expected.
The original data is:

id
x1
x1


x1

code:
data have1;
set have;
temp=lag(id);
if missing(id) then id=temp;
run;

The output shows:


id temp
x1
x1 x1
x1 x1

x1

I understand it is because the forth obs.temp is getting the value
from the previous id which is missing data. But how to revise the code
to make the forth id's value to be x1.

What's more, if I want to assign a MISSING value to a var. How should
I write the code?

saying

if _n_=5 then temp2 is missing;

Is the above code right?
Reeza
2011-01-11 19:05:42 UTC
Permalink
Post by palapara
thanks Reeza.
Somehow, it doesn't work as I expected.
id
x1
x1
x1
data have1;
        set have;
        temp=lag(id);
        if missing(id) then id=temp;
run;
id        temp
x1
x1         x1
x1         x1
x1
I understand it is because the forth obs.temp is getting the value
from the previous id which is missing data. But how to revise the code
to make the forth id's value to be x1.
What's more, if I want to assign a MISSING value to a var. How should
I write the code?
saying
if _n_=5 then temp2 is missing;
Is the above code right?
data want;
set have;

retain temp;

if not missing(lag(id)) then temp=lag(id);

if missing(id) then id =temp;



run;

The line above will work, but I'd sincerely hope you have another
criteria to filter on to delete the record. the _n_ counts the number
of loops, which is usually the number of observations, but not always,
depending on if you use the trailing @ or (i think) multiple set
statements.
Lou
2011-01-12 00:59:26 UTC
Permalink
Post by Reeza
Post by palapara
thanks Reeza.
Somehow, it doesn't work as I expected.
id
x1
x1
x1
data have1;
set have;
temp=lag(id);
if missing(id) then id=temp;
run;
id temp
x1
x1 x1
x1 x1
x1
I understand it is because the forth obs.temp is getting the value
from the previous id which is missing data. But how to revise the code
to make the forth id's value to be x1.
What's more, if I want to assign a MISSING value to a var. How should
I write the code?
saying
if _n_=5 then temp2 is missing;
Is the above code right?
data want;
set have;
retain temp;
if not missing(lag(id)) then temp=lag(id);
if missing(id) then id =temp;
run;
The line above will work, but I'd sincerely hope you have another
criteria to filter on to delete the record. the _n_ counts the number
of loops, which is usually the number of observations, but not always,
statements.
_n_ tracks the number of loops through the DATA step - it has nothing
directly to do with SET statements. Consider the following code:

DATA FEE;
DO N = 1 TO HOWMANY;
SET FIE NOBS = HOWMANY;
more SAS statements...
END;
STOP;
RUN;

You'll have gone through and processed the entire FIE dataset, however large
it is. But _n_ will get no higher than 1, because you've looped through the
DATA step only once.
Lou
2011-01-12 01:12:03 UTC
Permalink
Post by palapara
Hi there,
I think I have messed up the programming languages..
How to specify a var is null in SAS?
if id is null then id=lag(id);
is the above correct or should be something such as missing, etc...?
Thanks!
SAS doesn't use the term "null", and variables in SAS can't have null values
the way they do in, for instance, ORACLE. The closest thing in SAS to a
null value is a missing value.

To set a numeric variable to missing, use a period like this: a = .;

For character variables, missing values are spaces, so to set a character
variable to missing, do this: b = ' ';

But beware - unlike ORACLE, missing values compare in SAS - a missing value
is always the lowest possible value. If a and b are variables, and both are
missing values, then the condition IF A = B is true in SAS. In ORACLE, if
they're both null values, the condition is not true.

LAG: The LAG function does not return the value from the previous
observation, it return the value from the previous execution of the lag
function. In the statement you show in your post, the lag function would
execute only when ID was missing, so the only thing it could return is a
missing value.

Assuming ID is a numeric variable, I think what you want is something like

RETAIN HOLDID;
SET whatever;
IF ID ^= . THEN HOLDID = ID;
IF ID = . THEN ID = HOLDID;
palapara
2011-01-12 21:01:44 UTC
Permalink
Post by Lou
Post by palapara
Hi there,
I think I have messed up the programming languages..
How to specify a var is null in SAS?
if id is null then id=lag(id);
is the above correct or should be something such as missing, etc...?
Thanks!
SAS doesn't use the term "null", and variables in SAS can't have null values
the way they do in, for instance, ORACLE.  The closest thing in SAS to a
null value is a missing value.
To set a numeric variable to missing, use a period like this:  a = .;
For character variables, missing values are spaces, so to set a character
variable to missing, do this:  b = ' ';
But beware - unlike ORACLE, missing values compare in SAS - a missing value
is always the lowest possible value.  If a and b are variables, and both are
missing values, then the condition IF A = B is true in SAS.  In ORACLE, if
they're both null values, the condition is not true.
LAG:  The LAG function does not return the value from the previous
observation, it return the value from the previous execution of the lag
function.  In the statement you show in your post, the lag function would
execute only when ID was missing, so the only thing it could return is a
missing value.
Assuming ID is a numeric variable, I think what you want is something like
RETAIN HOLDID;
SET whatever;
IF ID ^= . THEN HOLDID = ID;
IF ID = . THEN ID = HOLDID;
Thanks Lou..It is very detailed and very helpful!
Cheers!
palapara

Loading...