Help using Base SAS procedures

using first. = . repedetively

Accepted Solution Solved
Reply
Super Contributor
Posts: 401
Accepted Solution

using first. = . repedetively

I have 3 first. criteria to use, but I must use ot on 14 different variables... is there a easy way to do this

if first.YEAR then price_GR=.;

if first.Type then price_GR=.;

if first.Store then price_GR=.;

I have 14 different price_GR=.;  (eg. UN_GR, GDP_GR, etc.)

Cant have 14 series of the first.YEAR, first.Type and first.Store .. Is there a easy way to make them repedetive rather than type each out?  thanks


Accepted Solutions
Solution
‎02-03-2012 06:00 PM
Regular Contributor
Posts: 184

Re: using first. = . repedetively

by year type store ;

if first.store then call missing (UN_GR, GDP_GR, etc) ;

View solution in original post


All Replies
PROC Star
Posts: 7,489

using first. = . repedetively

I'm not sure what you are trying to do.  You can always specify them as:

if first.YEAR then do;

  price_GR=.;

  etc.

  etc.

end;

if first.Type then do;

  etc.

  etc.

end;

etc.

You could also load your data into an array and, rather than have 14 statements, just refer to the variables in the array.

SAS Employee
Posts: 416

Re: using first. = . repedetively

Not sure if this is what you are looking for. Regards, Udo

 

proc sort data=sashelp.class out=have;

by sex age;

run;

data want;

set have;

if first.sex then call missing(sex,age,height,weight);

if first.age then call missing(sex,age,height,weight);

by sex age;

run;

Super Contributor
Posts: 401

Re: using first. = . repedetively

Let me try to explain it better:

Is there better way to write this?

if first.YEAR then price_GR=.;

if first.Type then price_GR=.;

if first.Store then price_GR=.;

if first.YEAR then UN_GR=.;

if first.Type then UN_GR=.;

if first.Store then UN_GR=.;

if first.YEAR then GDP_GR=.;

if first.Type then GDP_GR=.;

if first.Store then GDP_GR=.;

and so on 14 times with 14 different VARS_GR.... Thanks

PROC Star
Posts: 7,489

Re: using first. = . repedetively

Do those variables already exist and you just want them reset to missing?

Super Contributor
Posts: 401

Re: using first. = . repedetively

yes

PROC Star
Posts: 7,489

Re: using first. = . repedetively

Here is an example of how you could do it for, say, all numeric variables:

proc sort data=sashelp.class out=test;

  by age;

run;

data want;

  set test;

  array vars _numeric_;

  by age;

  if first.age then do over vars;

    call missing(vars);

  end;

run;

Respected Advisor
Posts: 4,173

Re: using first. = . repedetively

@ART

DO OVER?!

You definitely started with SAS some years ago. I believe DO OVER was already marked as "obsolete" in SAS V6 :smileylaugh:

In modern SAS you could write it with no explicit loop at all:

call missing(of _numeric_); 

PROC Star
Posts: 7,489

Re: using first. = . repedetively

Patrick,

Yes, I started with SAS a long time ago.  In fact, before it became a company!

However, I still like using do over and don't know why they stopped including it in the documentation.

Yes, in this case, your suggestion is all that would be needed.  But not quite so easy for other assignments. Unfortunately, they never included a similar call assign (e.g., call (assign,3, of _numeric_);

Solution
‎02-03-2012 06:00 PM
Regular Contributor
Posts: 184

Re: using first. = . repedetively

by year type store ;

if first.store then call missing (UN_GR, GDP_GR, etc) ;

Super User
Super User
Posts: 7,076

Re: using first. = . repedetively

As Howles has said already you do not need to test all of the higher level variables in the sort order.  If it the first for higher level term it also the first for the lower level term, by the definition of how FIRST. is calculated.  So it you just reset on the first. for the lowest level term it will also reset at the first for all of the higher level terms.

To reset a number of variables to missing use the call missing statement.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 207 views
  • 0 likes
  • 6 in conversation