DATA Step, Macro, Functions and more

DO loop troubles

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 107
Accepted Solution

DO loop troubles

Hi, 

 

I have a simple do-loop:

 

data nobs;   

    do i=i to 2;

data nobs_i;

   set nobs_i;

keep Nobsused;

end;

run;

 

It is giving me the following error msg: No Matching DO/SELECT statement. Why?

 


Accepted Solutions
Solution
‎03-16-2018 11:22 AM
Super Contributor
Posts: 320

Re: DO loop troubles

Hello,

If you goal is to understand do loops, you should start by looking at examples of its usage rather than trying to guess how they work.

Do loops are used inside data steps and not to iterate on several data steps, procs,...

If you are interested in code generation you should learn SAS macro language but a minimal command of the base language is required to avoid confusion.

View solution in original post


All Replies
Super User
Posts: 9,611

Re: DO loop troubles

The second data statement begins a new data step, so the do in the old one has no matching end. What are you trying to do?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 107

Re: DO loop troubles

Posted in reply to KurtBremser
I'm trying to figure out do-loops with a simple example. I have two datasets: nobs_1 and nobs_2. I would like to keep Nobsused in both datasets. Thanks
Super User
Posts: 9,611

Re: DO loop troubles

The basic step would be

data nobs;   
set
  nobs_1
  nobs_2
;
keep Nobsused;
run;

If you have an arbitrary number of datasets, use

set
  nobs_1-nobs_&n.
;

if your number of datasets is stored in macro variable n.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 107

Re: DO loop troubles

Posted in reply to KurtBremser
Ok, this is helpful. But what if I wanted to keep the two datasets separete?
Super User
Posts: 22,873

Re: DO loop troubles

Back to the original question then, what are you trying to accomplish here?


@GKati wrote:
Ok, this is helpful. But what if I wanted to keep the two datasets separete?



Frequent Contributor
Posts: 107

Re: DO loop troubles

I'm trying to figure out do-loops with a simple example. I have two datasets: nobs_1 and nobs_2. I would like to keep Nobsused in both datasets without merging the two datasets.

I am going to have about 16 different datasets that I need to modify. I need to keep them separately and run a code through them one-by-one.
Thanks
Super User
Posts: 22,873

Re: DO loop troubles

What is NObsUsed?


@GKati wrote:
I'm trying to figure out do-loops with a simple example. I have two datasets: nobs_1 and nobs_2. I would like to keep Nobsused in both datasets without merging the two datasets.

I am going to have about 16 different datasets that I need to modify. I need to keep them separately and run a code through them one-by-one.
Thanks

 

Frequent Contributor
Posts: 107

Re: DO loop troubles

It's variable.
Super User
Posts: 22,873

Re: DO loop troubles

I'm not a fan of twenty questions. Please explain your process in detail. 

The approach you're currently trying doesn't make sense for any process.


@GKati wrote:
It's variable.

 

 

Solution
‎03-16-2018 11:22 AM
Super Contributor
Posts: 320

Re: DO loop troubles

Hello,

If you goal is to understand do loops, you should start by looking at examples of its usage rather than trying to guess how they work.

Do loops are used inside data steps and not to iterate on several data steps, procs,...

If you are interested in code generation you should learn SAS macro language but a minimal command of the base language is required to avoid confusion.
Frequent Contributor
Posts: 107

Re: DO loop troubles

gamotte, Reeza

Thanks for your help. My intention was not to waste your time. I have been looking at do-loop examples and that is how I came up with the code above. My intuition comes from STATA, where it is possible to loop over different datasets and modify them without merging them. I now understand that that is not possible in SAS and that I need to write a macro to do that. I will try that and see how I do.
Thanks for your time.
Super User
Posts: 22,873

Re: DO loop troubles

If you state what you're trying to achieve we can recommend the best method. 

For example, PROC DATASETS is more efficient to modify data sets to drop/keep variables that multiple data steps. If you take the approach used in other programming languages and try and apply them to another language, you miss learning how to efficiently use the new language. 

 

Or that you can easily create a view with just the one variable that only gets created when called.

 


@GKati wrote:
gamotte, Reeza

Thanks for your help. My intention was not to waste your time. I have been looking at do-loop examples and that is how I came up with the code above. My intuition comes from STATA, where it is possible to loop over different datasets and modify them without merging them. I now understand that that is not possible in SAS and that I need to write a macro to do that. I will try that and see how I do.
Thanks for your time.

 

☑ This topic is solved.

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

Discussion stats
  • 12 replies
  • 120 views
  • 0 likes
  • 4 in conversation