BookmarkSubscribeRSS Feed
putnik
Calcite | Level 5
Hi, I have very limited knowledge of SAS and looking for help:
I have 3 col in table:

Year, State and Rate
2009 MA $150
2009 NH $80
2009 ME $55
2010 MA $155
2010 NH $87
2010 ME $60

I need to change the current Rates according to this statement:

if Year = 2009 and state is MA - Rate * 2
if Year = 2009 and state is NH - Rate * 3
if Year = 2009 and state is ME - Rate * 4
if Year = 2010 and state is MA - Rate * 1.5
if Year = 2010 and state is NH - Rate * 2
if Year = 2010 and state is ME - Rate * 2.5

Any ideas? Thank you
3 REPLIES 3
AndyJ
Fluorite | Level 6
data sample_data;
infile datalines;
input year state $ rate;
datalines;
2009 MA 150
2009 NH 80
2009 ME 55
2010 MA 155
2010 NH 87
2010 ME 60
;
run;

data modify;
set sample_data;

select(state);
when ('MA') do;
if year=2009 then rate=rate*2;
else if year=2010 then rate=rate*1.5;
end;
when ('NH') do;
if year=2009 then rate=rate*3;
else if year=2010 then rate=rate*2;
end;
when ('ME') do;
if year=2009 then rate=rate*3;
else if year=2010 then rate=rate*2.5;
end;
otherwise;
end;

run;
putnik
Calcite | Level 5
Thank you so much!
Cynthia_sas
Diamond | Level 26
Hi:
As an alternative, I like the PROC FORMAT approach.
cynthia
[pre]
data rates;
infile datalines;
input year state $ rate comma8. ;
return;
datalines;
2009 MA $150
2009 NH $80
2009 ME $55
2010 MA $155
2010 NH $87
2010 ME $60
;
run;

proc format;
value $rmult '2009MA' = '2'
'2009NH' = '3'
'2009ME' = '4'
'2010MA' = '1.5'
'2010NH' = '2'
'2010ME' = '2.5';
run;

data newrate;
length testval $6;
set rates;
testval = catt(put(year,4.0),state);
mult = input(put(testval,$rmult.),best8.);
newrate = rate * mult;
run;

ods listing;
proc print data=newrate;
run;
[/pre]

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1343 views
  • 0 likes
  • 3 in conversation