BookmarkSubscribeRSS Feed
scb
Obsidian | Level 7 scb
Obsidian | Level 7

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.

6 REPLIES 6
Shmuel
Garnet | Level 18

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.

DanielSantos
Barite | Level 11

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

gamotte
Rhodochrosite | Level 12

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;
Kurt_Bremser
Super User

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
Shmuel
Garnet | Level 18

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;
Shmuel
Garnet | Level 18
The benefif of using format is in case you have a new or unknown brand without discount, you will not get an error.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 906 views
  • 0 likes
  • 5 in conversation