BookmarkSubscribeRSS Feed
CathyVI
Pyrite | Level 9

Hi,

I am doing inflation cost analysis which am new to using sas. I will like to produce an output for all cost variables but I only have output for total.

Please any help with how the code can generate the outputs e.g. inf_cost_hosp, inf_cost_ed,

inf_cost_rx and inf_cost_out and not only the inf_cost_total?

Here is the code:

 

%macro cost(var1);
data a2007_2011;
set b2007_2011;
if obsyear='2006' then inf_cost_&var1.=cost_&var1.*1.10;
else if obsyear='2007' then inf_cost_&var1.=cost_&var1.*1.08;
else if obsyear='2008' then inf_cost_&var1.=cost_&var1.*1.04;
else if obsyear='2009' then inf_cost_&var1.=cost_&var1.*1.04;
else if obsyear='2010' then inf_cost_&var1.=cost_&var1.*1.01;
else inf_cost_&var1.=cost_&var1.;
run;
%mend cost;
%cost(hosp);
%cost(ed);
%cost(out);
%cost(rx);
%cost(total);

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Using a macro seems unnecessary and inefficient here. I have also written the code assuming year is a numeric variable, which it should be. You will have to somehow create a list of variables in the ARRAY statement, if their names all begin with COST_ then it's pretty simple.

 

data want;
     set b2007_2011;
     if obsyear=2006 then factor=1.1;
     else if obsyear=2007 then factor=1.08;
     else if /* you type the rest */ ;
     array cost cost_:;
     do i=1 to dim(cost);
         cost(i)=factor*cost(i);
     end;
run;

 

--
Paige Miller
Reeza
Super User
data want;
     set b2007_2011;
     array _factor[2006:2010] _temporary_ (1.10, 1.08, 1.04, 1.04, 1.01)
     _year = input(year, 8.);
     array cost cost_:;
     do i=1 to dim(cost);
if 2006<=year<=2010 then factor=_factor(year);
else factor=1; cost(i)=factor*cost(i); end; run;

Adding the factors into an array makes it easier as well.

Reeza
Super User

And how to make your code actually work as is. You were always starting from the base data set, overwriting any previous changes. To have the changes stick, you need to use the same data set. 

 

%macro cost(var1);
data b2007_2011;
set b2007_2011;
if obsyear='2006' then inf_cost_&var1.=cost_&var1.*1.10;
else if obsyear='2007' then inf_cost_&var1.=cost_&var1.*1.08;
else if obsyear='2008' then inf_cost_&var1.=cost_&var1.*1.04;
else if obsyear='2009' then inf_cost_&var1.=cost_&var1.*1.04;
else if obsyear='2010' then inf_cost_&var1.=cost_&var1.*1.01;
else inf_cost_&var1.=cost_&var1.;
run;
%mend cost;

data b2007_2011;
set a2007_2011;
run;


%cost(hosp);
%cost(ed);
%cost(out);
%cost(rx);
%cost(total);

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
  • 3 replies
  • 347 views
  • 1 like
  • 3 in conversation