Help using Base SAS procedures

Simple manipulation of multiple datasets

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 107
Accepted Solution

Simple manipulation of multiple datasets

Hi,

 

I have the following code, which I need to run multiple times (i=1 to 100) on different datasets. I need to save the datasets separately. How can I do this most efficiently?

Thanks,

 

****************************************

data claims_1;

input client year cost contract;

datalines;

 

18 2011 589.92 10.2

19 2010 1629.8 11.5

20 2011 1813.29 2.3

21 2012 412.06 3.2

22 2012 219.82 0.4

23 2010 3669.98 5.9

24 2012 4879.63 6.8

;

run;

 

/*GENERAL HOSPITALS*/

proc glm data=claims_1;

class year (ref='2010' );

model cost=year|contract/ solution;

ods output ParameterEstimates=work.parmest_1 fitstatistics=fit_1 nobs=nobs_1;

run;

data parmest_1;

set parmest_1;

rename Parameter=Variable;

run;

data parmest_1;

set parmest_1;

numord+1;

if first.variable then numord=1;

run;

data nobs_1;

set nobs_1;

if label^="Number of Observations Used" then delete;

keep Nobsused Variable numord;

Variable="N. Obs.";

rename NObsUsed=Estimate;

numord=2000;

run;

data fit_1;

set fit_1;

keep RSquare RootMSE;

run;

proc transpose data=fit_1 out=fit_1;

run;

data fit_1;

set fit_1;

if _Name_="RSquare" then numord=1000;

if _Name_="RootMSE" then numord=1001;

rename _Label_=Variable;

drop _name_;

rename col1=Estimate;

run;

data work.parmest1;

set work.parmest_1;

run;

 

** append parmest_1;

proc datasets;

append base=work.parmest1 data=work.nobs_1 force;

run;

append base=work.parmest1 data=work.fit_1 force;

run;

quit;

 

 


Accepted Solutions
Solution
‎03-17-2018 07:17 PM
Super User
Posts: 23,791

Re: Simple manipulation of multiple datasets

Options generally go at the top of your program. 

 

options mprint symbolgen;

%macro demo (n=);

%do i=1 %to &n;

%put &n;
%put &n_new;

%end;

%mend;

%demo(n=8);

View solution in original post


All Replies
Frequent Contributor
Posts: 107

Re: Simple manipulation of multiple datasets

This is what i have so far. This doesn't produce any output, without any error messages.

 

%macro do_all;

%do i=1 %to 2;

 

data work.parmest_&i_new;

set work.parmest_&i;

rename Parameter=Variable;

run;

data parmest_&i;

set parmest_&i;

numord+1;

if first.variable then numord=1;

 

run;

data nobs_&i;

set nobs_&i;

if label^="Number of Observations Used" then delete;

 

keep Nobsused Variable numord;

Variable="N. Obs.";

 

rename NObsUsed=Estimate;

numord=2000;

 

run;

data fit_&i;

set fit_&i;

keep RSquare RootMSE;

run;

proc transpose data=fit_&i out=fit_&i;

run;

data fit_&i;

set fit_&i;

if _Name_="RSquare" then numord=1000;

if _Name_="RootMSE" then numord=1001;

 

rename _Label_=Variable;

drop _name_;

rename col1=Estimate;

run;

data work.parmest1;

set work.parmest_&i;

run;

** append parmest_&i;

proc datasets;

append base=work.parmest1 data=work.nobs_&i force;

run;

append base=work.parmest1 data=work.fit_&i force;

run;

quit;

%end;

%mend do_all;

 

Super User
Posts: 23,791

Re: Simple manipulation of multiple datasets

[ Edited ]

Run it with the following options: 

 

options mprint symbolgen;

And make sure you call the macro. 

There's no %do_all; at the end that would indicate you ran the macro code. 

 

EDIT: 

And in general, don't do this:

 

data work.parmest_&i_new;

set work.parmest_&i;

 

I's in the middle make it harder to manage, and when you put it in the middle you need a colon to identify the end, otherwise, how does SAS know if the macro variable is:

 

&i or &i_new?

 

 

 

 

Frequent Contributor
Posts: 107

Re: Simple manipulation of multiple datasets

Thx for your reply.
1. where do I put this option? Before the first data step?If so, I'm still getting error msgs "The symbol is not recognized and will be ignored."

2. I got this now, thx
Solution
‎03-17-2018 07:17 PM
Super User
Posts: 23,791

Re: Simple manipulation of multiple datasets

Options generally go at the top of your program. 

 

options mprint symbolgen;

%macro demo (n=);

%do i=1 %to &n;

%put &n;
%put &n_new;

%end;

%mend;

%demo(n=8);
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 232 views
  • 1 like
  • 2 in conversation