SAS Macro Language

18 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.96
List Price:  €92.81
You save:  €27.84
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$106.02
List Price:  A$151.47
You save:  A$45.44
S$94.41
List Price:  S$134.88
You save:  S$40.47
HK$546.80
List Price:  HK$781.18
You save:  HK$234.37
CHF 63.34
List Price:  CHF 90.49
You save:  CHF 27.15
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.42
List Price:  NZ$166.33
You save:  NZ$49.90
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,416.31
List Price:  ₨27,738.77
You save:  ₨8,322.46
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$356.70
List Price:  B$509.60
You save:  B$152.89
R1,295.44
List Price:  R1,850.72
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪259.50
List Price:  ₪370.74
You save:  ₪111.23
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,712.31
List Price:  ¥15,303.96
You save:  ¥4,591.65
MX$1,187.89
List Price:  MX$1,697.07
You save:  MX$509.17
QR254.57
List Price:  QR363.69
You save:  QR109.12
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.64
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,764.23
List Price:  ISK kr13,949.49
You save:  ISK kr4,185.26
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.59
List Price:  ден5,712.52
You save:  ден1,713.92
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,305.79
List Price:  Ft36,152.68
You save:  Ft10,846.88
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
Already have an account? Log In

Transcript

Welcome to clinical data management program using SAS. In this video we will be discussing about SAS macro language. What is SAS macro language? It's a programming language for string manipulation. It consists of strings as characters using SAS stat statements. SAS statements are pieces of SAS statements.

Normally it is considered as advanced concepts and not difficult. So basically, SAS has a powerful programming feature called macros, which allows us to avoid repetitive sections of code, and to use them again and again when needed. It also helps create dynamic variables within the code that can take different values for different run instances of the same code. macros can also be declared for blocks of code, which will be reused multiple times in a similar manner to macro variables. Now, why do we use macros? macros are harder to write than standard code that just like normal codes are most similar to its various simpler to read normal quotes, but macros a bit difficult but then it is very much less time consuming.

If you make one change then the whole task can be performed. It's a reusable code it can make programs data driven. What are the differences between macros and macro variables macro variable starts with and or ampicillin. It's a single character value. And macros starts with percentage it's a piece of a program may use macro statements often uses macro variables. Now basically what our macro programs macros macros are basically macro programs that is it is a group of statements that is referred by a name and to use it in program or to use it in any program anywhere using that name.

It starts with percentage macro statement. So macro starts with percentage, and it ends with percentage main means the end of the program next the scope of macro variables. Now there are two types of macro variables. Run is your local variable and other is your global variable. Now what is local macro variable what is global macro variable? First is local macro variable which is defined within a macro, it can only be used in that macro that is local macro variables can be accessed by SAS programs in which they are declared as part of the program.

They are typically used to supply different variables to the same SAS statements and they can process different observations of data set. Now what is global macro variables global macro variables can also be defined in open code it can also be used anywhere so global macro variables are called global macro variables because they can be accessed by any SAS program available in the SAS environment. In general, they are the system assigned variables which are accessed by multiple programs. Our general example of a global macro variable can be like sis data sustained that is a distributed system data system. So now let's move to the past. session that is how do we use macro in SAS.

So first before we start doing macro practically we have to create our library to access our data sets, so we'll do lip nim CDM within double quotes will give the path of our data sets. So let's run the live name statement. So this is my CDM library consisting of the data sets. So now let's start doing macros practically so I'll give you a few examples of how to do macros or how to write macro programs in SAS. First, we'll be creating a macro variable using percentage lead statement. So for that we are doing this macro percentage layer let new or newer is a variable name.

I'm giving the variable name as someone who will be using this later on in our program. So first, we are declaring the macro over here. Now let's first start with our data. Step because we have to use this variable in our data set. So say we are adding data a new one set CDM dot disease, we are using the same data set called disease. I hope you all remember what was the disease data set all about.

It consists of around 2000 observations and it consists of variables like ID gender, date of birth, zip code, employment status, education, marital status, children ancestry, average commute, daily interest use available vehicles military service disease. So here I have named the I have given the name of my new data set that is data a new one, and this is the name of my original data set. So now I'm going to use the macro and percent new var, that is the name of the macro variable. I hope y'all remember that when a variable starts with an ampersand means it's a macro variable and when it starts with a person It does it's a macro program or macros we can call it this. So, this is the name of min macro variable cause ampicillin new bar equals to say I want to sum up average commute and daily internet use and then we have to give a proper statement to see the data portion of the data set, proper data will be new one obviously, because we are doing the manipulation in new one data set and Linda So, basically here first we have created the macro variable we are seeing that new what is the main macro variable which we are creating which is equal to some bar and we are performing in addition of average commute and daily interviews in my data set called New one Now first let's run this code.

So, see over here, here the name of my macro variable is somewhere as newer equals to somebody have given somebody some average commute and daily internet use. Now, suppose we need to change the name of the variable. So, we need not change it within the DATA step. Just if we make the changes as say, we make it as, say var seven, then the variable name will be automatically changed. So let's run the code again and you will understand See, non st have some very it has become about seven. So this is my users macro.

Once you create the macro, just you can change the name and be done. You did not write the whole program again and again. Now. We'll again create Now I'll show you how to create a macro to transform our variable. So for that, we'll be creating macros, we'll be writing macro programs. So percentage macro, as I told you, if you if your program starts with a structured percentage, that means you're creating macros.

My function is transfer variable. So transform this is a function and transforming the variable called x. Okay, now I have to name my macro variables, so I have to give amberson me meters x dot sum. That is I'm going to perform a sum between average commute and daily internet use. Again, we're going to add these two variables, even add any of the variables my main, I want to show you how to transform a variable here we're basically doing addition. Similarly if you want to do difference, we'll do x dot difference.

These are the name of the macro view. So average commute minus daily intermittent use, then. So once your macro program is end, you can write percentage meant. So we have to write percentage men. Now you need to incorporate this in your data set, so we have to form a new data set. So we need a data step so I'm forming a new data set called Data new to set CDM or disease I'm going to use the same data set which we had used before.

Now I'm going to perform addition as well as I will also perform subtraction so my first macro will be percentage transform this may be even easier Give us one for in percentage from transform this this was the name of the function saving variable name and give it as var five and then in order to display the data portion of the data set you have to obviously do properly so proper data equals tuning to vendre so let's run this whole things first. See we are going to transform our variable x we have created some macro variable extract some which is sum of average commute and daily internet use. Extra difference which is the difference between average commute and daily internet use and after we enter macro we write percentage here, transform this is basically we are using this function D let's have a written percentage transform this for the variable workforce and percentage transform this way But about faith and we are using this on our data set new to now let's display the data portion of the data set.

See here what for some has come. What will differences come similarly workflow if someone's come in perfect differences, because we have used two variables so x is this key word for and verify or for some invalid for difference we'll have some graphic difference some is some average commute in Dalian to induce differences again, the difference between average comedian interviews are five songs same average commuting daily interview some of and radical differences, average commute minus daily internet use. Now, let's do some other types of transformations. Say we want to find a square or find a Q find squared find q find inverse of a particular variable, then how do we do that using macro So again, that's a form of transformation. So we'll be using percentage macro transform I'm giving the name of the function as transform this one you can give any name to your function.

Now to create those macro variables, so it should start with Amazon. First, feeding the variables a squared, which is equals to m percent x x to the power two. Similarly, ampersand x dot underscore cube will be equal to ampersand x part 370 If we want to do inverse it is ampersand x dot underscore inverse equals ampersand x to the back three sorry, Amazon not Amazon x inverses one divided by m percent x one divided by m percent x. And then so your macros are formed now we have two into the macros we have red men, two percentage men transform this so my macros is created. Now I'm going to apply this in my data set. So data new three m forming set CDM dot disease this this is my original data set new trees may duplicate data set duplicate home of my disease data set.

Now I have to use the macro I call the macro video percentage transform this I want to perform this function on which variable i want to perform it on. Say any variable numeric variables event takes are taken over here average coming. And then so obviously want to understand the result, you need to see the data portion of the dataset. So PROC PRINT data equals new three and then run so let's run this code So, see this is average commute square square of the variable average commute average commute q that is to have the variable consider the battery. And this is the average commute in was there is one way average commute. So the this way you do the transformations now I'll be showing you how to create a macro to run the contents procedure and the print procedure in any data set.

Similarly, we'll be doing percentage macro, my name of the macro will be contents of so obviously I'm going to apply it in any data set. So I'm writing it as DSM no proc, contents data equals ampersand DSM. So basically, whatever you write in place of in case the data The contents for that dataset will be displayed. First I'm going to create a macro. So, this is my end of the macro now, and write percentage meant contents. So percentage contents of semicolon.

So, this is my whole macro program. Now we need to apply this so in order top line will write percentage contents is the name of my macro contents of then within bracket will give the name over it as et CIE So, I can give Sam giving you three if you want for another data set you can write new to us from any other data set which you want. So, here you can change the name of the data set just and your work program will be again executed You need not write separately a proper sentence procedure for each interview So once you just create a macro program and just change the name over here, and your program can be executed. So now let's run this code. See, the contents procedure that is a descriptive portion of the data set is displayed. You got it for the variable for the data set new three, this is of new two.

And then you have the for the video for the data set new three, these mini observations are there. These are that critical lists of variables and attributes for the data sets are given. Similarly, you can do it the same thing for property owner differences there you have possession, you can just change the name of the function and obviously the program. So I'll just show you the macro that you need to write for PROC PRINT instead of contents of who's going to print off this PROC PRINT data. This will be print off you can give any name for your macro print is used to display the data portion of the data set. So let's run this code so this is the data portion of the data set you voted for new three as well as new to whichever data sets names are given.

I'll just close the result viewer and run the code once more so that you can see the results clearly. See this the data perform that it doesn't. That's so do you have process macro process macro language so In this video we're here to this was me lad. This was the last topic for medical data management program. I hope you're like the course and the program you understood everything. If you have any doubts, you're free to clarify your doubts.

Thank you. Goodbye. Have a nice day. See you on the next program.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.