Skip to content
Snippets Groups Projects
Commit 6fe6349c authored by Angie Liu's avatar Angie Liu
Browse files

Upload New File

parent f6a9ea8e
No related merge requests found
/* ----------------------------------------------------- */
/* AEDE 6120 */
/* Lab 7 */
* the IF statement;
* DO LOOP;
* MACRO;
/* ----------------------------------------------------- */
/* clear the work library */
proc datasets library=work kill noprint;
run;
quit;
/* clear log window */
dm 'log;clear;';
*the IF statement;
*IF <expression1> THEN <statement>;
*IF <expression1> THEN <statement>; ELSE <statement>;
*IF <expression1> THEN <statement>; ELSE IF <expression2> THEN <statement>; ELSE <statement>;
*creating dummy var using IF statement;
data new1;
set sashelp.citiyr;
if pan>240000 then high_pan=1;
else high_pan=0;
run;
*creating categorical vars using IF statement;
data new2;
set sashelp.citiyr;
if pan>240000 then pan_level=3;
else if pan<230000 then pan_level=1;
else pan_level=2;
run;
*deleting data using IF statement;
data new3; set sashelp.citiyr;
if pan<230000 then delete;
run;
/**************************** DO LOOP ****************************/
* syntax;
* do index = start_value to end_value;
* end;
data new_dt;
do i = 1 to 15;
y = i*2;
output;
end;
run;
data account;
balance = 500;
do yr = 1 to 6;
balance + 1000;
end;
run;
* DO UNTIL vs DO WHILE;
data loan1;
loan = 20000;
payments = 0;
do until (loan = 0);
loan = loan - 1000;
payments = payments + 1;
end;
run;
data loan2;
loan = 20000;
payments = 0;
do while(loan > 0);
loan = loan - 1000;
payments = payments + 1;
end;
run;
/**************************** MACRO ****************************/
* we can think of it as a function;
* f(x) = 3x + 5;
* f(8);
* (1) Creating a Macro program;
* %MACRO MacroName(Param1, Param2, ...);
* Macro Statements;
* %MEND;
* (2) Calling a Macro program;
*
* %MacroName(Value1, Value2, ...);
* Example - macro I;
%macro prnt(var_name);
proc print data=sashelp.citiyr;
var &var_name;
sum &var_name;
title "Sum of &var_name";
run;
%mend prnt;
* call the macro;
%prnt(panf);
%prnt(panm);
* Example - macro II;
%macro show_result(car_make, car_type);
proc print data = sashelp.cars;
where make = "&car_make" and type = "&car_type" ;
title "Car Sales of &car_make &car_type";
run;
%mend;
* call the macro;
%show_result(Acura, Sedan);
%show_result(BMW, SUV);
* Example - macro and loop I;
%macro counti(finish);
%let i = 1;
%do %while (&i <= &finish);
%put the value of i is &i;
%let i=%eval(&i+1);
%end;
%mend;
* call the macro;
%counti(5);
%counti(8);
* Example - macro and loop II;
data work.citi;
set sashelp.citiyr;
run;
* syntax for creating lags;
* new1 = lag(oldv);
* new2 = lag2(oldv);
%macro lag_var(vname);
data citi;
set citi;
%do i = 1 %to 4;
*create lagged vars;
&vname._l&i = lag&i(&vname);
*create differenced var;
&vname._d = dif(&vname);
*create lags for the differenced var;
&vname._dl&i = lag&i(&vname._d);
%end;
run;
%mend;
* call the macro;
%lag_var(PAN);
%lag_var(PANF);
%lag_var(PANM);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment