Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SP21 AEDE6120
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Angie Liu
SP21 AEDE6120
Commits
6fe6349c
Commit
6fe6349c
authored
4 years ago
by
Angie Liu
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
f6a9ea8e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
LabCode/lab_7.sas
+213
-0
213 additions, 0 deletions
LabCode/lab_7.sas
with
213 additions
and
0 deletions
LabCode/lab_7.sas
0 → 100644
+
213
−
0
View file @
6fe6349c
/* ----------------------------------------------------- */
/* 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
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment