## if first.var1 and first.var2 and first.var3 then

Solved
Frequent Contributor
Posts: 89

# if first.var1 and first.var2 and first.var3 then

Hello ,

Is it possible to extend use First.variable options  in SAS. I have 5 variables . 4 of them are grouping variable and last one is count variable. I want to show count of first observation of each group. below is my code;

data test;

set test;

by var1 var2 var3;

if first.var1 and first.var2 and first.var3 and first.var4 then value6= var5;

else value6 = .;

above code just used if first.var1 condition only. I am wondering if I am doing something wrong. If so if there is other way to fix this issue,

Thank you

Accepted Solutions
Solution
‎07-31-2013 04:28 PM
Super User
Posts: 8,267

## Re: if first.var1 and first.var2 and first.var3 then

If you can tell us what you want then we can try to see how (of if) you can do it using FIRST. processing.

What do you mean by "count of first observation of each group"?

If you want to assign each distinct set of values of var1 to var4 a unique identifier then you  would do:

data want ;

set have;

by var1 var2 var3 var4 ;

groupnum + first.var4 ;

run;

All Replies
Super User
Posts: 23,980

## Re: if first.var1 and first.var2 and first.var3 then

Print out your first. and last. variables to see what they are to help determine your logic.

e.g.

data test;

set test;

a1=first.var1;

a2=first.var2;

....

b1=last.var1;

b2=last.var2;

run;

Then look at the rows you want to keep and see what your logic should be.

Super User
Posts: 23,980

## Re: if first.var1 and first.var2 and first.var3 then

If you do what I suggest I bet you'll figure out you need only either:

first.var1

OR

first.var4

It's rare to need the and/or combinations of them but there are cases. If you still can't figure it out, post sample data. You'll find if you post sample data it forces you to be more clear with your question, but you'll also get responses much faster...and useful ones instead of guesses.

Super User
Posts: 8,267

## Re: if first.var1 and first.var2 and first.var3 then

(first.var1 and first.var2 and first.var3 and first.var4) is the same as (first.var1) by definition.

Similarly (first.var1 or first.var2 or first.var3 or first.var4) is the same as (first.var4).

Frequent Contributor
Posts: 89

## Re: if first.var1 and first.var2 and first.var3 then

Tom,

I got exactly same(first.var1 and first.var4)  for (or and and ) condition.  as you said.Is there a way to achieve my desire output.

thanks

Solution
‎07-31-2013 04:28 PM
Super User
Posts: 8,267

## Re: if first.var1 and first.var2 and first.var3 then

If you can tell us what you want then we can try to see how (of if) you can do it using FIRST. processing.

What do you mean by "count of first observation of each group"?

If you want to assign each distinct set of values of var1 to var4 a unique identifier then you  would do:

data want ;

set have;

by var1 var2 var3 var4 ;

groupnum + first.var4 ;

run;

Frequent Contributor
Posts: 89

## Re: if first.var1 and first.var2 and first.var3 then

Tom,

That was what exactly I needed. I was trying to find total number of residents for given metropolitan area (FIPS Code) with categorical demographics

thank you for your help Tom,

🔒 This topic is solved and locked.