DATA Step, Macro, Functions and more

value from a data file to a macro variable

Reply
Contributor scb
Contributor
Posts: 69

value from a data file to a macro variable

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.

Trusted Advisor
Posts: 1,579

Re: value from a data file to a macro variable

[ Edited ]

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.

Super Contributor
Posts: 474

Re: value from a data file to a macro variable

[ Edited ]

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

Regular Contributor
Posts: 234

Re: value from a data file to a macro variable

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;
Super User
Posts: 7,832

Re: value from a data file to a macro variable

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
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Trusted Advisor
Posts: 1,579

Re: value from a data file to a macro variable

Posted in reply to KurtBremser

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;
Trusted Advisor
Posts: 1,579

Re: value from a data file to a macro variable

The benefif of using format is in case you have a new or unknown brand without discount, you will not get an error.
Ask a Question
Discussion stats
  • 6 replies
  • 154 views
  • 0 likes
  • 5 in conversation