DATA car;
infile CARDS;
INPUT BRAND $ MODEL $ PRICE;
CARDS;
Audi ABC 20000
Audi ABB 25000
BMW CDE 40000
BMW CEE 46000
;
run;
DATA DISC;
infile CARDS;
INPUT BRAND $ DISCOUNT;
CARDS;
Audi 0.9
BMW 0.95
;
run;
Data car;
set car;
if BRAND eq 'Audi' then price * &discount;
if BRAND eq 'BMW' then price * &discount;
run;
May I know how can pass the value from a data file (Disc dataset) to a macro variable then use it in the above program? Thanks.
As there are different BRANDS and each have different DISCOUNT
you better merge or sql join discount dataset with cars dataset by BRAND;
You may also want create a format to get the discount per brand.
Hi.
Just merge/join the two data sets by BRAND.
proc sql noprint;
create table car as
select a.*, a.PRICE * b.DISCOUNT as DISCOUNT_PRICE
from car as a left join disc as b
on a.BRAND = b.BRAND;
quit;
Hope it helps.
Daniel Santos @ www.cgd.pt
Though @DanielSantos solution is to be preferred, here is a way to do it with macro-variables :
DATA DISC;
infile CARDS;
INPUT BRAND $ DISCOUNT;
call symput(cats("discount_",BRAND),DISCOUNT);
CARDS;
Audi 0.9
BMW 0.95
;
run;
Data car;
set car;
DISCOUNT=symgetn(cats("discount_",BRAND));
DISCOUNT_PRICE=PRICE*DISCOUNT;
run;
Here's the data step method:
data car;
input brand $ model $ price;
cards;
Audi ABC 20000
Audi ABB 25000
BMW CDE 40000
BMW CEE 46000
;
run;
data disc;
input brand $ discount;
cards;
Audi 0.9
BMW 0.95
;
run;
proc print data=car noobs;
run;
data car;
merge
car (in=a)
disc (in=b)
;
by brand;
if a;
if b then price = price * discount;
drop discount;
run;
proc print data=car noobs;
run;
Output:
brand model price Audi ABC 20000 Audi ABB 25000 BMW CDE 40000 BMW CEE 46000 brand model price Audi ABC 18000 Audi ABB 22500 BMW CDE 38000 BMW CEE 43700
Tested code using format:
DATA car;
infile CARDS;
INPUT BRAND $ MODEL $ PRICE;
CARDS;
Audi ABC 20000
Audi ABB 25000
BMW CDE 40000
BMW CEE 46000
XXX YY 50500
;
run;
DATA DISC;
infile CARDS;
INPUT BRAND $ DISCOUNT;
CARDS;
Audi 0.9
BMW 0.95
;
run;
data cntl;
length start end $16 label $10;
set disc end=eof;
retain fmtname '$disc';
start = brand; end=start;
label = put(discount,z4.2);
output;
if eof then do;
label = '1.00';
hlo = 'O';
output;
end;
run;
proc format cntlin=cntl; run;
/*proc format cntlout=cntl1; select $disc; run;*/
data car_new;
set car;
price_new = price * input(put(brand,$disc.),best4.2);
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.