BookmarkSubscribeRSS Feed
raivester
Quartz | Level 8

I wish to perform the same procedure on a number of different data sets that all differ in name by a single naming element. I would like to run this using a loop, but am not sure how. I'd like to do the following, but do it for each of four data sets: data01, data02, data03, data04 and using a loop to read in each new data set. (This a simplified example of what I actually want to do with the data, but I am really only struggling to figure out how the loop works.

 

proc print data=data01; run;
3 REPLIES 3
PaigeMiller
Diamond | Level 26

No loops needed.

 

data all;
    set data: indsname=indsname;
    dsname=indsname;
run;
proc print data=all;
    by dsname;
    pageby dsname;
run;
--
Paige Miller
Reeza
Super User

Often that requires macros but the first step is to have working code that works for a base case. If you're only changing the name of the data set that's actually quite a simple use case.

 

Usually that gets you into macro language though, here's some references for you:

 

UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

Examples of common macro usage - e.g. looping using an index or data set list
https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

ballardw
Super User

Here is an approach to developing something like you describe.

The first data step creates small elements of code at a time and writes them out. The result, barring syntax errors, could be copied into the editor and run.

The second makes a longer element to write to the results window and could also be copied to the editor to run. One alternate is to write to a specific output file instead of the File Print, such as File "c:\folder\mycodefile.sas" ;

then use  " %include "c:\folder\mycodefile.sas" ; after the data step to call the written code. This has an advantage of creating a code file you can reuse or edit.

The last example shows use of the Call Execute to place lines directly into the execution queue to run after the data step completes.

 

data _null_;
   file print;
   do i=1 to 4;
      dsname = cats("data",put(i,z2.));
      put "proc print data =";
      put dsname;
      put ";";
      put "run;";
   end;
run;

/* slightly fancier*/
data _null_;
   file print;
   length longstring $ 150;
   do i=1 to 4;
      dsname = cats("data",put(i,z2.));
      longstring = catx(' ', "proc print data =", dsname,';');
      put longstring;
      put "run;";
   end;
run;      

/* and to execute */
data _null_;
   file print;
   length longstring $ 150;
   do i=1 to 4;
      dsname = cats("data",put(i,z2.));
      longstring = catx(' ', "proc print data =", dsname,';');
      call execute(longstring);
      Call execute( "run;";);
   end;
run;  

Variations on this theme.

Have parts of your needed code like names or options in variables and use the concatenation functions/operators to build of syntax strings.

 

Note, if you aren't aware of it, SAS iterated Do loops in a data step can take character values when listed.

Do i='first', 'second', 'third';

in the above code would create data set names: datafirst, datasecond, datathird but the concatenation would use just the variable i instead of put(i, z2.) which was intended to duplicate your shown 01, 02 etc suffixes.

 

I would strongly suggest using one of the methods that write to a text program file and examine your generated code prior to executing it until you get some experience with the technique.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 507 views
  • 0 likes
  • 4 in conversation