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

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?

 

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12
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

12 REPLIES 12
GKati
Pyrite | Level 9
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
Kurt_Bremser
Super User

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.

GKati
Pyrite | Level 9
Ok, this is helpful. But what if I wanted to keep the two datasets separete?
Reeza
Super User

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?



GKati
Pyrite | Level 9
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
Reeza
Super User

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

 

GKati
Pyrite | Level 9
It's variable.
Reeza
Super User

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.

 

 

gamotte
Rhodochrosite | Level 12
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.
GKati
Pyrite | Level 9
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.
Reeza
Super User

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.

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 12 replies
  • 1294 views
  • 0 likes
  • 4 in conversation