BookmarkSubscribeRSS Feed
iank131
Quartz | Level 8
Hi,

I have a dataset called DataSetA which has n observations. I have a smoothly running sas programme called ProgB. ProgB takes one observation at a time of DataSetA, beginning with the first one, does a whole bunch of stuff and updates DataSetB with a new record or changes an existing record. What I want to do is execute ProgB once for each observation of DataSetA. I am doing this manually at the moment by changing a macro variable, which is the observation number of DataSetA: manually updating the observation number: 1, 2, 3, ...,n and manually pressing the SAS programme execution button each time. I would like to automate this simple process, but I don't know how. Could you please help me?

I use PC SAS. I looked at some available tutorials and tips on SAS macros and functions, but I am overwhelmed by all the information for what I think is a really simple automation task. I have read that macros and functions cannot accommodate data steps, even the rather simple ones that I have in my ProgB (but I could be wrong). Any tips on what I can do?

Algorithmically, all I want do is:

Do i = 1 to n;
GetNextDataSetAObsvn(i); /* puts DataSetA observation i in a table with only one observation */
ExecuteProgB; /* executes lots of data steps, procs, etc on this single obsvn and updates DataSetB with the result */
End;

But I don't know how to do this in PC SAS, even after trying for many hours!

Thanks in advance,
Ian.
4 REPLIES 4
Cynthia_sas
SAS Super FREQ
Hi:
This is an excellent paper to get you started:
http://www2.sas.com/proceedings/sugi28/056-28.pdf

In particular, look at some of the later examples (Steps 7, 8, and 9), which show how to automate a DATA step and a PROC step in a macro program. Of course, you will have to understand steps 1 through 6 - -but the examples are quite good and very easy to understand.

cynthia
iank131
Quartz | Level 8
> Hi:
> This is an excellent paper to get you started:
> tp://www2.sas.com/proceedings/sugi28/056-28.pdf


Hi Cynthia,

Many thanks for the paper reference. I worked through all the examples and learned a lot more from it than many other papers I looked at because this one was really step by step. Combining what I learned from this paper with the other reply I got, I have been able to solve my problem after more than a month of work on it! Thanks very much for your fast response and help!

Best wishes,
Ian.
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Iank131,
If I understood you correctly this is what you need (I used SASHELP.CLASS as an example);
[pre]
%let dsn=SASHELP.CLASS;
%macro a;
data _null_;
set &dsn obs=n end=e;
if e then call symputx('n',n);
run;
%do i=1 %to &n;
data _a;
set &dsn;
if _n_=&i;
run;

%end;
%mend a;
%a
[/pre]
Sincerely,
SPR
iank131
Quartz | Level 8
> %let dsn=SASHELP.CLASS;
> %macro a;
> data _null_;
> set &dsn nobs=n end=e;
> if e then call symputx('n',n);
> run;
> %do i=1 %to &n;
> data _a;
> set &dsn;
> if _n_=&i;
> run;
>
> %end;
> %mend a;
> %a

Hi SPR,

Many many thanks for your generous help on my problem! Combined with the paper Cynthia referred me to and your suggested code, it worked brilliantly! I solved a long-standing issue I had and learned a great deal more about macros.

Thanks so much! and also for the how fast you responded!!
Regards,
Ian.

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!

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
  • 4 replies
  • 938 views
  • 2 likes
  • 3 in conversation