BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kelSAS
Obsidian | Level 7

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

@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 🙂 Thank you!

 

 

 

 

View solution in original post

13 REPLIES 13
novinosrin
Tourmaline | Level 20

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

Reeza
Super User

Maybe a view?

 

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

proc means data=myView;
run;
kelSAS
Obsidian | Level 7

Yes! I want to see only those observations printed.

Astounding
PROC Star

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.

novinosrin
Tourmaline | Level 20

@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 🙂 Thank you!

 

 

 

 

data_null__
Jade | Level 19

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;
novinosrin
Tourmaline | Level 20

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

kelSAS
Obsidian | Level 7

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

art297
Opal | Level 21

@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

 

novinosrin
Tourmaline | Level 20

@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. 

ballardw
Super User

@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);

novinosrin
Tourmaline | Level 20

@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. 

kelSAS
Obsidian | Level 7

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. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 8601 views
  • 2 likes
  • 7 in conversation