Help using Base SAS procedures

proc print a few observations

Accepted Solution Solved
Reply
Highlighted
Occasional Contributor
Posts: 14
Accepted Solution

proc print a few observations

I understand using firstobs=x, and obs=x, to print out all variables for that observations. I am trying to use proc print for several observations that are far from each other in observation numbers. So, for example, if I want observations number 38701, I would do

proc print data=dataset (firstobs=38701, obs=38701).

 

Would anyone know how to print two or three observations that have observations numbers like 38701, 14080, and/or 9631. Thank you for your time in advance. 


Accepted Solutions
Solution
Thursday
PROC Star
Posts: 1,569

Re: proc print a few observations

@kelSAS   It's a very interesting question, at-least to me.  I wonder why there isn't an option to filter obs within a proc print step. We can concur with the suggestions using 2 step solution.

 

I would like something like where obs in (1,5,8,...........n) ;

when we know the following works,, I would vote for your idea to have some obs or n within proc print to filter using random lists

 

data w;
set sashelp.class;
n+1;
run;

proc print data=w;
where n in (1,5,9);
run;

I am keen to see an opinion of the sas artist and king @art297 and @data_null__  respectively if they have the time Smiley Happy Thank you!

 

 

 

 

View solution in original post


All Replies
PROC Star
Posts: 1,569

Re: proc print a few observations

Do you mean filter a list (in (38701,14080,9631) in a proc print?

Super User
Posts: 23,260

Re: proc print a few observations

Posted in reply to novinosrin

Maybe a view?

 

data myView / view=myView;
set sashelp.class;
if _n_ in (13, 12, 4);
run;

proc means data=myView;
run;
Occasional Contributor
Posts: 14

Re: proc print a few observations

Posted in reply to novinosrin

Yes! I want to see only those observations printed.

Super User
Posts: 6,629

Re: proc print a few observations

If you don't want to run multiple PROC PRINTs, you would first need to construct a data set holding the desired observations.  For example:

 

data want;

do k=38701, 14080, 9631;

   set have point=k;

   output;

end;

stop;

run;

 

proc print data=want;

run;

 

Be sure to include that STOP statement.  Without it, the top DATA step turns into an infinite loop.

Solution
Thursday
PROC Star
Posts: 1,569

Re: proc print a few observations

@kelSAS   It's a very interesting question, at-least to me.  I wonder why there isn't an option to filter obs within a proc print step. We can concur with the suggestions using 2 step solution.

 

I would like something like where obs in (1,5,8,...........n) ;

when we know the following works,, I would vote for your idea to have some obs or n within proc print to filter using random lists

 

data w;
set sashelp.class;
n+1;
run;

proc print data=w;
where n in (1,5,9);
run;

I am keen to see an opinion of the sas artist and king @art297 and @data_null__  respectively if they have the time Smiley Happy Thank you!

 

 

 

 

Respected Advisor
Posts: 3,845

Re: proc print a few observations

Posted in reply to novinosrin

I think @Astounding has the right idea using point.  This will pull the OBS in the order specified.  I would add a variable to ID the OBS number.  If a data set REMOVED observations you would need to check _ERROR_ and handle appropriately.

 

data obsV / view=obsV;
   length _OBS_ 8;
   do point=1000,456,354;
      set sashelp.heart point=point;
      _obs_ = point;
      output;
      end;
   stop;
   run;
proc print n width=min;
   run;
PROC Star
Posts: 1,569

Re: proc print a few observations

Posted in reply to data_null__

Thank you so much for your time. Much appreciate it.

Occasional Contributor
Posts: 14

Re: proc print a few observations

Posted in reply to novinosrin

Thank you so much for your active input. I wish there was one step, but your two-step works perfectly.

PROC Star
Posts: 8,146

Re: proc print a few observations

Posted in reply to novinosrin

@novinosrin: I don't have anything to add to what has already been suggested other than I would create and store a macro (e.g., %printn) if this is a task that one has to repeat. E.g., using @data_null__'s suggested code:

%macro printn(dsn,obs);
  %let obs = %sysfunc(tranwrd(%quote(&obs.),%str( ),%str(,)));
  data obsV / view=obsV;
    length _OBS_ 8;
    do point=&obs.;
      set &dsn point=point;
      _obs_ = point;
      output;
    end;
    stop;
  run;
  proc print n width=min;
  run;
  proc datasets memtype=view nodetails nolist;
   delete obsV;
  run;
%mend printn;

%printn(sashelp.heart,1000 456 354)

Art, CEO, AnalystFinder.com

 

PROC Star
Posts: 1,569

Re: proc print a few observations

@art297  Thank you sir for your time. It feels very satisfying to receive thoughtful responses from my most favorite people that I mentioned 2 here +Ksharp(didn;t bother this time as I assumed sleep time in china can be awkward).

 

Anyways, the point is although this seems pretty basic in essence to have a more than one step solution and then do a proc print. I kinda really felt intrigued OP's perspective of having this non sequential filter facility enabled within a proc print. (aka IN operator using the obs column)

 

I encountered a similar situation at my work before I quit to pursue Grad school on a requirement to do list pull for eligibility customers on a waterfall method.  Back then I didn't pay much attention. But When i do envision the requirement becoming increasingly common, that facility could be a better utility. -- >my 2 cents. 

Super User
Posts: 13,304

Re: proc print a few observations


@kelSAS wrote:

I understand using firstobs=x, and obs=x, to print out all variables for that observations. I am trying to use proc print for several observations that are far from each other in observation numbers. So, for example, if I want observations number 38701, I would do

proc print data=dataset (firstobs=38701, obs=38701).

 

Would anyone know how to print two or three observations that have observations numbers like 38701, 14080, and/or 9631. Thank you for your time in advance. 


I would ask how do you know that you want observations 38701, 14080 and 9631?

And what happens if the data is sorted or the number of records changes? The observation numbers would or could change.

 

I would suggest turning the rule(s) you use to say that you need specific observations into syntax that can use a WHERE statement such as here:

proc print data=sashelp.class;
  Where (sex='F' and age=13)
      or name='John';
run;

OR add the observation number as a variable, obsnum for example, and use a WHERE statement to reference that variable such as:

 

where obsnum in (38701, 14080, 9631);

PROC Star
Posts: 1,569

Re: proc print a few observations

@ballardw  Sir, I think OP is intuitively after your obsnum or my demo in one proc print step. If my understanding is right, OP perhaps wants that to happen in one print step. 

Occasional Contributor
Posts: 14

Re: proc print a few observations

Thank you for asking that question. I need to handle outliers properly and when I run 'proc univariate', the output would show the maximum and minimum with observation number and the value of the variable I ran 'proc univariate' with. I could use certain value to limit data that's proc print'ed, but was wondering if I could print several datalines by observation number in one proc print step. 

☑ This topic is solved.

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

Discussion stats
  • 13 replies
  • 253 views
  • 1 like
  • 7 in conversation