turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- BI
- /
- Enterprise Guide
- /
- if first.var1 and first.var2 and first.var3 then

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-31-2013 01:42 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to learner_sas

07-31-2013 04:28 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to learner_sas

07-31-2013 02:01 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

07-31-2013 04:33 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to learner_sas

07-31-2013 03:41 PM

(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).

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-31-2013 03:52 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to learner_sas

07-31-2013 04:28 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-31-2013 04:50 PM

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,