How to loop through data by Observation?

Reply
New Contributor
Posts: 3

How to loop through data by Observation?

I'm trying to take a dataset and take the first n observations and take their name and use that to do something else. For example 

 

OBS        Name   

1               jkg       
2               ddg  
3               sdfs 
4               asdf 
5               sadf 
6               rjkr 

 

Because the names are different I want to call them by observation number (i.e. Obs[1] for jkg) and then loop through the names to created subsetted data in another dataset. So Obs[1] should get me jkg and I should then be able to search another dataset for jkg and subset all values with the name to another dataset. Then it should go to Obs[2] to ddg and do the same, and then continue. 

 

I know this can be done by just searching the whole dataset for anything with the name of interest but even if I created a macro, I think there should exist a more succinct way of doing it but nothing I'm trying is really working.

Respected Advisor
Posts: 4,640

Re: How to loop through data by Observation?

You could try indexed access in your other dataset.

 

Another approach is to join (or merge) dataset and other dataset on (by) Name. 

PG
Super User
Posts: 5,069

Re: How to loop through data by Observation?

It sounds like you are making this difficult by insisting on separate subsets for each NAME.  Why not create one larger subset with all 10 names?  The extract would look something like this:

 

data subset;

set all_names (obs=10 keep=name);

run;

 

proc sql;

create table want as

select * from another_dataset where name in (select name from subset) order by name;

quit;

 

That (or something very much like it) should give you the subset with all records matching the first 10 names.  And in order.  So subsequent processing could use a BY statement:

 

proc reg data=want;

by name;

....

 

The BY statement lets you loop through a separate analysis for each NAME, while keeping all your data together in one data set.

Super User
Super User
Posts: 7,392

Re: How to loop through data by Observation?

I am not sure I follow what your asking?   The only reason jkg is at logical observation number 1 is because it is not sorted, so why would you assume that logical observation 1 = jkg?  This doesn't make any sense as a change to the dataset could corrupt your logic.  Your logic should work on the data, not on an absract concept.  As @Astounding has stated, if you just want the first 10 obs then use that command, if you specifically want data items, then use a where clause.

New Contributor
Posts: 3

Re: How to loop through data by Observation?

I know Obs[1] is only that way because that's the original way it was outputted. That's what I want. So I should be able to output and input differet datasets so when it goes through it takes those positions from the dataset and the names associated with them. So if in the first dataset jkg is Obs[1] in one set, then if in a different set Obs[1] is something called 'hggg' it should instead search the larger dataset for all of those inputs instead.  In this way if I have different 'top 10' lists based on different criteria then I shouldn't have to go in and change the code dramatically. Ideally I could just load this into a macro and switch out names for datasets to avoid code clunkiness. 

Super User
Posts: 5,069

Re: How to loop through data by Observation?

It's a little difficult to tell what you are asking for, but this might be a step in the right direction.

 

%macro subset (obsno);

 

data subset;

retain _obsno_ &obsno;

if _n_=1 then set have point=_obsno_;

set larger_dataset;

if variable_from_larger_dataset = name;

drop _obsno_ name;

run;

 

%mend subset;

 

%subset (5)

 

When calling the macro with OBSNO=5, the program gets the 5th observation where NAME is "sadf" and looks for all matching observations from your larger data set.

 

 

Ask a Question
Discussion stats
  • 5 replies
  • 345 views
  • 0 likes
  • 4 in conversation