Sas Macro Examples


Published on

Learn SAS programming, SAS slides, SAS tutorials, SAS certification, SAS Sample Code, SAS Macro examples,SAS video tutorials, SAS ebooks, SAS tutorials, SAS tips and Techniques, Base SAS and Advanced SAS certification, SAS interview Questions and answers, Proc SQL, SAS syntax, Advanced SAS, Quick links, SAS Documentation, SAS Addin to Microsoft office, Oracle, ODS HTML, ODS< Clinical trials, Financial Industry, Q & A, SAS Resumes, SAS Blogs,,

Published in: Education, Technology
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Sas Macro Examples

  1. 1. SAS macro to Create / Remove a PC Directory...<br />Here's a SAS macro to Create and Remove a PC Directory... Often we ignore Notes and warning in the SAS log when we try to create/remove a directory that does/doesn't exist...This macro first checks for the existence of the directory and then create/delete it or else put a message to the SAS log...try it out :-) /* Macro to Create a directory */<br />%macro CheckandCreateDir(dir); <br />   options noxwait; <br />   %local rc fileref ; <br />   %let rc = %sysfunc(filename(fileref,&dir)) ;    <br />   %if %sysfunc(fexist(&fileref)) %then <br />      %put The directory " &dir" already exists ; <br />   %else <br />     %do ; <br />         %sysexec mkdir " &dir" ; <br />         %if &sysrc eq 0 %then %put The directory &dir has been created. ; <br />Read more @ <br />Ways to Count the Number of Obs in a dataset and pass it into a macro variable...<br />Well...there are many ways of getting the observation count into a macro variable...but there a few pros and cons in those methods...1. using sql with count(*)..  eg.         proc sql;              select count(*) into :macvar             from dsn;         quit; pros: simple to understand and develop cons: you need to read the dataset in its entirety which requires processing power here...2. datastep  eg.        data new;          set old nobs=num;          call symputx('macvar',num);       run;<br />Read more @ <br />SAS macro to split dataset by the number of Observations specified<br />Suppose there was a large dataset....This SAS macro program splits the dataset by the number of observations mentioned...macro name%split(DatasetName, No ofobservation to split by)/* creating a dataset with 100000 observations*/<br />data dsn;<br />do i=1 to 100000;<br />output;<br />end;<br />run;<br />%macro split(dsn,splitby);<br />data _null_;<br />set &dsn nobs=num;<br />Read more @ <br />SAS macro to reorder dataset variables in alphabetic order...<br />How do you reorder variables in a dataset...I get this many a times.... <br />Here's a macro for you to achieve it...For example I've used a dataset sashelp.flags and created some more variables with variety of variables with names upper / lower cases and _'s to demonstrate the reorder macro....   Please try this macro for yourself and let me know your suggestions....<br /> <br />/* Example dataset with variety of variable names */ <br /> <br />data flags; <br />set sashelp.flags; <br />a=2; <br />b=4; <br />Read more @<br />Use SAS function Propcase() to streamline Google Contacts<br />You might think I am crazy...but I have been using this macro for a long time to fix some contacts in my Google Contacts...I get a little irritated when I can't find a particular person by I wrote this macro...This macro takes for Input a .csv file that is exported from Google Contacts and outputs a file that is ready to be imported to Google Contacts....often I wanted to have Names in the proper case...Try it yourself and let me know if it needs any tweaks...Propcase in SAS Documentation.<br />%macro organizeGoogleContacts(infile,outfile);<br />/*Import your contacts into SAS */ <br />data googlegroups;<br />infile " &infile" dlm=',' dsd lrecl=32767 firstobs=2;<br />Read more @ <br /> SAS Macro to Create a delimited text file from a SAS dataset...<br />A document that discusses SAS Macro to Create a delimited text file from a SAS data set.. <br />options mprint;<br /> <br />data one;<br />  input id name :$20. amount ;<br />  date=today();<br />  format amount dollar10.2 <br />           date mmddyy10.;<br />  label id=" Customer ID Number" ;<br />datalines;<br />1 Grant   57.23<br />2 Michael 45.68<br />3 Tammy   53.21<br />;<br />Read more @ <br />SAS Macro to split a dataset into multiple datasets vertically with a common primary key<br />This macro splits a dataset to multiple datasets vertically with a common primary key. For eg, a dataset has 400 fields and 20,000 records. If we can split the dataset into two, with 200 fields and 20,000 records in each dataset with a common field like loan number as primary key would be helpful to load the details for analysis.<br /> <br />/**<br />To be called like this...<br />%splitdsnverticallykey(dsn,varperdsn,keyvars=);<br />eg. %splitdsnverticallykey(sashelp.vtable,4,keyvars=memname libname);<br /> <br />Where -----------<br /> <br />dsn - libname.datasetname to be split<br />varperdsn - How many vars per dsn excluding the key variables<br />keyvars - specify the primary key variables<br />*/ <br />Read more @ <br />SAS Macro that reads the filenames available at a particular directory on any FTP server (i.e. Windows Network Drive/Unix/Mainframe)<br />Here's a macro that reads the filenames available at a particular directory on any FTP server (i.e. Windows Network Drive/Unix/Mainframe)...<br />For Windows network drives we use the Filename Pipe Statement<br />For Mainframe and Unix we use the FileName FTP protocol statement.<br />For further reference please refer to Filename statements in SAS Documentation.<br />First we need to create 2 Excel files <br />ServerList.xls – 3 columns with servertype | host | sourcedir<br />Read more @<br />