SAS Tutorial | How to Create Macro Variables and Use Macro Functions

Sdílet
Vložit
  • čas přidán 15. 07. 2024
  • Using the SAS Macro Language, you can produce SAS programs that are easier to modify and maintain - or better yet, programs which modify and maintain themselves. In this SAS How to Tutorial, Stacey demonstrates using SAS macro variables to store text and substitute values dynamically in your program.
    Download Data Files
    See SASHELP data sets in SAS.
    Chapters
    00:00 - Overview of what you’ll learn
    01:04 - What macro variables are and how you can create them
    05:57 - Manipulating macro variables with macro functions
    10:19 - Creating macro variables from data using SQL
    14:58 - Using the trimmed option
    Learn more about SAS Software
    ◉ SAS Macro Language 1: Essentials Course - support.sas.com/edu/schedules...
    ◉ Getting Started with the Macro Facility - go.documentation.sas.com/?doc...
    ◉ SAS Macro Language Documentation - go.documentation.sas.com/?cdc...
    ◉ Passing comma-delimited values into SAS macros and macro functions -
    blogs.sas.com/content/sgf/201...
    ◉ Tip: %LET macro statement - communities.sas.com/t5/SAS-Co...
    ◉ How to create and use SAS macro functions - blogs.sas.com/content/sgf/202...
    Related Videos
    ◉ SAS Enterprise Guide: Macro Variables - • SAS Tip | SAS Enterpri...
    Beyond the Basics of Macro - • SAS Tutorial | Beyond ...
    ◉ Getting the Most out of SAS Macro and SQL - • SAS Tutorial | Getting...
    %SURVEYCORRCOV
    ◉ Macro: Complex Survey Data Correlations for Multivariate Analysis and Model Building - • %SURVEYCORRCOV Macro: ...
    SUBSCRIBE TO THE SAS USERS CZcams CHANNEL #SASUsers #LearnSAS
    czcams.com/users/SASUsers?sub_...
    ABOUT SAS
    SAS is a trusted analytics powerhouse for organizations seeking immediate value from their data. A deep bench of analytics solutions and broad industry knowledge keep our customers coming back and feeling confident. With SAS®, you can discover insights from your data and make sense of it all. Identify what’s working and fix what isn’t. Make more intelligent decisions. And drive relevant change.
    CONNECT WITH SAS
    SAS ► www.sas.com
    SAS Customer Support ► support.sas.com
    SAS Communities ► communities.sas.com
    SAS Analytics Explorers ► explorers.sas.com
    Facebook ► / sassoftware
    Twitter ► / sassoftware
    LinkedIn ► / sas
    Blogs ► blogs.sas.com
    RSS ► www.sas.com/rss
  • Věda a technologie

Komentáře • 111

  • @staceysyphus8238
    @staceysyphus8238 Před 4 lety +33

    Thanks so much for taking the time to watch this video tip! This is probably my favorite topic to teach about the SAS language. I'd love to hear your thoughts and questions :)

    • @ruhannegi8376
      @ruhannegi8376 Před 4 lety

      Hi thanks q lot for explaining Macros in such a easy way.. can you please explain how to export print tables generated by Proc Tabulate in excel format and each table in different sheets.
      Thanks in advance for your help.

    • @SASUsers
      @SASUsers  Před 4 lety

      We're checking on this for you!

    • @ruhannegi8376
      @ruhannegi8376 Před 4 lety

      @@SASUsers thanks a lot for you reply. Waiting for the answer.😀

    • @staceysyphus8238
      @staceysyphus8238 Před 4 lety +2

      @@ruhannegi8376 I found this discussion with an accepted solution on our SAS Communities page. I hope this helps: communities.sas.com/t5/General-SAS-Programming/Proc-Tabulate-to-excel/td-p/405465

    • @mail2valan
      @mail2valan Před 4 lety

      love to watch this.. very useful. Thank you so much

  • @patricklewis7981
    @patricklewis7981 Před rokem +2

    You have given a great introduction to Macros.

    • @SASUsers
      @SASUsers  Před rokem

      Thank you so much for the feedback!

  • @2beCuil
    @2beCuil Před 2 lety +1

    Thank you for this tutorial

  • @yoannkouame3282
    @yoannkouame3282 Před 3 lety +3

    STACEY always doing great with SAS, so nice to have you as instructor!

    • @SASUsers
      @SASUsers  Před 3 lety

      We think so, too! Thanks for sharing!

  • @thepineweaver
    @thepineweaver Před 3 lety +4

    Very clear material - one of my favorite SAS instructors.

    • @SASUsers
      @SASUsers  Před 3 lety

      Ours, too! ;) Thanks for watching!

  • @annawells1913
    @annawells1913 Před rokem +1

    excellent video, thank you!

  • @talie6236
    @talie6236 Před 4 lety +2

    You are one awesome teacher. Very articulate, friendly, knowledgeable and beautiful ☺️.

  • @niyazshaik4178
    @niyazshaik4178 Před 4 lety +2

    Thank you so much stacey..i learned base sas programming-1 from your e learning which is available in sas website...i really enjoy your classes...god bless you stacey.

  • @himself9278
    @himself9278 Před 4 lety +2

    Hi Stacey many thanks for the great presentation, I just love the flow of the info. I love using Macro, as it simplifies a code , #manipulating code

  • @benobasa1968
    @benobasa1968 Před 4 lety +3

    That's my SAS tutor on Cosera. I am excited to see her here!

    • @staceysyphus8238
      @staceysyphus8238 Před 3 lety

      Glad you're going through the Coursera courses! Those allow you to dive deep into SAS programming :)

  • @chunleizhang77
    @chunleizhang77 Před rokem

    Excellent video with bells and whistles. Thanks, Stacey!

    • @SASUsers
      @SASUsers  Před rokem +1

      Awesome feedback, and thanks for watching!

  • @sarajitsil
    @sarajitsil Před 4 lety +1

    Hi Stacey, a very helpful tutorial indeed! Many thanks for making this content.

    • @SASUsers
      @SASUsers  Před 4 lety

      Absolutely! We are glad you found it helpful!

  • @EderbalFilho
    @EderbalFilho Před 4 lety +4

    Excellent teacher !

  • @parulanand5967
    @parulanand5967 Před 3 lety +2

    Very useful and easy approach. Loved the way you explain !

    • @SASUsers
      @SASUsers  Před 3 lety

      Parul, thank you so much for your feedback! We are glad you found it helpful!

  • @sharangnimonkar9183
    @sharangnimonkar9183 Před 2 lety +1

    Hi Stacey,
    Really great way of explanation.
    Systematic approach made it easy to comprehend.

    • @SASUsers
      @SASUsers  Před 2 lety

      Thanks so much for the feedback Sharang! Glad you found it helpful!

  • @jalwerdt85
    @jalwerdt85 Před 4 lety

    Thank you! You made it so easy to understand.

    • @SASUsers
      @SASUsers  Před 4 lety

      Awesome! Glad to hear it!

  • @dominicmutiso1502
    @dominicmutiso1502 Před rokem +1

    Wow😂 @7:42 to 7:43. I love the effect- "reaction"

  • @omadnomad1635
    @omadnomad1635 Před rokem

    You are an amazing teacher . Love all your classes

    • @SASUsers
      @SASUsers  Před rokem

      So glad you enjoyed the tutorial! 🙂

  • @steveellison8686
    @steveellison8686 Před 2 lety

    Great instructor- I love your delivery!

    • @SASUsers
      @SASUsers  Před 2 lety

      So glad you enjoyed it! 🙂

  • @anvarisamukhamedov4961
    @anvarisamukhamedov4961 Před 3 lety +1

    Thanks for this session, now I understand how and where to use a macro variable. You are a very beautiful, friendly, and knowledgeable teacher.

    • @SASUsers
      @SASUsers  Před 3 lety +1

      Glad it was helpful!

    • @anvarisamukhamedov4961
      @anvarisamukhamedov4961 Před 3 lety

      @@SASUsers do you have the full courses of SAS for both beginners and advanced, I would like to have it for my personal learning.
      Many Thanks.

    • @SASUsers
      @SASUsers  Před 3 lety

      Anvar, thank you for your interest! We do have recommended courses by learning paths that have both beginner, intermediate, and advanced courses 2.sas.com/6056H3ujo

  • @oluwaledurojaye74
    @oluwaledurojaye74 Před rokem

    GRAET INSTRUCTOR, always enjoy your teaching on sas macro.

  • @ARSagar-dt7yj
    @ARSagar-dt7yj Před 3 lety +4

    This is an amazing video and simple to understand the importance and beauty of manipulating code. Would love to learn from you more. Please make a video of DO Loop and Arrays if possible. i became ta big fan to the way you explain it

    • @SASUsers
      @SASUsers  Před 3 lety

      A.R. thank you so much for the feedback! Check out our SAS Community pages for more resources on using Arrays and DO Loops 2.sas.com/6051GfvKB

  • @Ujwala1010
    @Ujwala1010 Před 3 lety +1

    Well explained 👏

  • @SoleLo
    @SoleLo Před rokem

    Recently started using Macro language and what a game changer… %include has been my favorite

    • @SASUsers
      @SASUsers  Před rokem

      Thanks for sharing, Jamarcus!

  • @JC-wf7gr
    @JC-wf7gr Před 4 lety +6

    I love her courses on Coursera

    • @octaviopalma1309
      @octaviopalma1309 Před 4 lety +1

      can u share me the link on Coursera pls?

    • @sydznl
      @sydznl Před 4 lety

      Yes me too

    • @staceysyphus8238
      @staceysyphus8238 Před 4 lety +1

      Here is link to the SAS Programmer Certificate program on Coursera: www.coursera.org/professional-certificates/sas-programming. You can also take advantage of a 30-day free trial to access many of our eLearning courses from our SAS site: www.sas.com/en_us/training/offers/free-training.html.

  • @dylanr494
    @dylanr494 Před 2 lety

    Great tutorial!

    • @SASUsers
      @SASUsers  Před 2 lety

      We're glad you found it helpful!

  • @pamelyn186
    @pamelyn186 Před 2 lety

    Amazing!

  • @goo6341
    @goo6341 Před 3 lety

    Great 👍 lesson. Thank you 🙏

  • @mekidesbekele1787
    @mekidesbekele1787 Před 3 lety +1

    you make it very easy

  • @sunilgupta2156
    @sunilgupta2156 Před 4 lety

    Nice video on SAS macros.

    • @SASUsers
      @SASUsers  Před 4 lety

      Glad you enjoyed it. These may be of interest, if you like blogs 2.sas.com/6056GbFsk

  • @mstshandu
    @mstshandu Před 2 lety

    Great presentation that is easy to understand

    • @SASUsers
      @SASUsers  Před 2 lety

      We love that you love it! Thanks for sharing, and hope you come back for more! Happy Holidays!

  • @mattyyyCodes
    @mattyyyCodes Před 2 měsíci

    The hearts for the favorite 3rd section made me laugh (:

  • @darshmathur4870
    @darshmathur4870 Před 4 lety +1

    Loved it short and crisp. Please make a video on user defined macro functions. Would appreciate it 😊.

    • @SASUsers
      @SASUsers  Před 4 lety

      We're glad you enjoyed the content and have shared your feedback with our Video team!

    • @staceysyphus8238
      @staceysyphus8238 Před 4 lety

      Great suggestion... It may take some time to produce a video on this topic, but in the meantime check out this helpful blog post: blogs.sas.com/content/sgf/2020/04/22/how-to-create-and-use-sas-macro-functions/

  • @papachoudhary5482
    @papachoudhary5482 Před 4 lety +2

    Thanks

  • @himself9278
    @himself9278 Před 4 lety +2

    Wow this is great, now i know how to remove the leading and trailing spaces, that results as result of creating a macro variable in proc sql, #trimmed

  • @mekidesbekele1787
    @mekidesbekele1787 Před 3 lety +1

    Awesome

  • @mekidesbekele1787
    @mekidesbekele1787 Před 3 lety

    encouraging!!!!

  • @ruhannegi8376
    @ruhannegi8376 Před 3 lety

    Hi thanks for wonderful video. Can we also put count of rows in dataset in to a macro variabke and use it in other programmes???

    • @SASUsers
      @SASUsers  Před 3 lety

      Ruhan, thank you for your inquiry! We are researching this for you!

    • @SASUsers
      @SASUsers  Před 3 lety

      Ruhan, Here is the Q&A that was posted in a previous SAS Community:
      2.sas.com/6055ybPHR
      The answer is yes, this can be done and it can be accomplished a multitude of ways. The above community post just shows a few examples, however, the first answer with PROC SQL isn't a complete solution because it doesn't put the number of rows in a macro variable. One would need to use the into clause to do that. Here is an example:
      proc sql;
      select count(*) as countrec into :Totobs from 2.sas.com/6056ybPHr;
      quit;
      %put &=Totobs;
      We also cover this along with a lot of other great macro programming skills and techniques in the SAS Macro Language 1: Essentials (2.sas.com/6057ybPHT) course.

  • @ruhannegi8376
    @ruhannegi8376 Před 4 lety

    Hi. Excellent video on macros. Thanks a lot. I have one question: I am producing a report using Proc Tabulate for data set. If I dont want to show some segments and only few segments in my report.is it possible?? For example: one of the columns is cars(5 different type) and another is number of cars sold.
    I am producing summary by car and % of each of cars sold. So is it possible in my report I show results only fir 3 types of cars and not for 5

    • @SASUsers
      @SASUsers  Před 4 lety +1

      We are checking into this for you!

    • @ruhannegi8376
      @ruhannegi8376 Před 4 lety +1

      @@SASUsers thanks a lot.😀

    • @SASUsers
      @SASUsers  Před 4 lety +1

      Ruhan,
      This feedback area isn't best suited to post detailed code examples. It appears that your question does not really involve the use of Macro variables or Macro functions to accomplish. You could use a WHERE statement with PROC TABULATE to limit the number of values for a column that went into your TABULATE step. For example:
      where model in ('BMW', 'Acura', 'Volvo');
      If you have specific usage question about PROC TABULATE using your data, your best resources are the SAS Community Forums 2.sas.com/6057GTPRc and SAS Technical Support 2.sas.com/6059GTPRl

  • @enssea.hamimed
    @enssea.hamimed Před 2 lety

    How about Mixed models?

  • @himself9278
    @himself9278 Před 4 lety

    Could you be having a link to where i can get the updated functions in SaS

    • @SASUsers
      @SASUsers  Před 4 lety

      Thanks for your inquiry! You can find some New Functions and Call Routines in documentation 2.sas.com/6055GkT8f

  • @maycrow1522
    @maycrow1522 Před 4 lety

    I'm using SAS EG, I wanted to practice along you, I cannot get your proc gplot statement to work, hbar and xaxis do not seem to be recognized... is there a different syntax with EG?

    • @SASUsers
      @SASUsers  Před 4 lety

      Hello May! This video uses SAS Studio as the SAS interface. Because you are using SAS Enterprise Guide, you may want to try this video instead: SAS Tip | SAS Enterprise Guide: Macro Variables 2.sas.com/6051GP1xv

    • @staceysyphus8238
      @staceysyphus8238 Před 4 lety

      @@SASUsers Hi May - the syntax should be exactly the same, regardless of if you are using SAS Studio or SAS Enterprise Guide. I'm using PROC SGPLOT in this example, which is included with Base SAS. Looks like maybe you are missing the 'S'? There is an older procedure named GPLOT, but it requires SAS/Graph and has different syntax.

  • @hkbca
    @hkbca Před 3 lety

    Can macro var values be used directly as numerical condition?

    • @SASUsers
      @SASUsers  Před 3 lety

      Hemant, Hi:
      Please go back and re-watch the video and pay particular attention to the section between time stamp 4:00 -5:33. In that part of the video, you will see the %LET statement for maxprice being set to 30000. This macro variable will be referred to as &MAXPRICE and it will be used anyplace in the code where the number 30000 is needed. Notice that the macro variable is used in several places. In the title, the &maxprice macro variable is used to explain the selection condition. However, in the WHERE statement for PROC SGPLOT and for PROC MEANS, since the MSRP variable is a regular numeric variable, it must be compared to a another numeric variable or a numeric constant. In this case, the 30000 is a numeric constant and the value 30000 is being supplied by using the &MAXPRICE macro variable. Later in the video, the value for &MAXPRICE is set to 40000 and used again. This is exactly the kind of usage you're asking about. Notice that when a macro variable is going to be treated as a regular number in SAS code, it is not quoted. We cover this topic in more depth in our SAS Macro Language 1 and Macro Language 2 classes.

  • @HimaPatel-kx8ic
    @HimaPatel-kx8ic Před měsícem

    hello i have a question entry level SAS programmers should learn both base and macro SAS???i fell bit difficult to memorise all the techniques you were using to code.

    • @SASUsers
      @SASUsers  Před měsícem

      You may find this handy for recommended learning paths for programmers 2.sas.com/6058ew6zr.

  • @priyankaveerapaneni3245
    @priyankaveerapaneni3245 Před 9 měsíci

    Hi Team,
    I'm trying to understand the below macro, Could any one of you explain please ?
    %macro mysum(n);
    %put mysum called: n=&n;
    %if &n>1 %then %eval(&n +%mysum(%eval(&n-1)));
    %else &n;
    %mend;
    %put %mysum(4);
    Result is : 10, but I want to know how the %mysum wiht %eval with in the same macro is executed ?
    Thank you so much in advance!! Looking forward for the explanation.

    • @SASUsers
      @SASUsers  Před 9 měsíci

      Unfortunately, we cannot provide detailed help or code or screenshots in this CZcams feedback area.
      If you have a SAS site license at your company, you have 2 options for detailed support: 1) open a case with SAS Tech Support at support@sas.com and send them your site license number, the code you've tried and a small sample of your data so they can help you with your question; or 2) post your question in the SAS Community Forum for Statistical Procedures here: 2.sas.com/6051u3ChT .
      We hope this helps!

    • @priyankaveerapaneni3245
      @priyankaveerapaneni3245 Před 9 měsíci

      @@SASUsers Thank you, will post it in the sas community forum.

  • @prasannadillikar7760
    @prasannadillikar7760 Před 3 lety +1

    Hi Stacey i attended your coursera courses on getting started with SAS which was really good. now i am learning some advance concepts like this but reaching at some error in my codes not able to debug it can you help me out with it.
    %MACRO PRAC(PRICE=MSRP,TYPE=SMALL LARGE HEAVY SIXWHEELS);
    DATA SAMPLE;
    SET SASHELP.CARS(KEEP=&PRICE);
    %IF 10280.00

    • @SASUsers
      @SASUsers  Před 3 lety +1

      We're looking into this for you! Thanks for your patience!

    • @SASUsers
      @SASUsers  Před 3 lety +1

      Hi, Prasanna! Here's what we were able to determine for you:
      This falls outside the scope of the Getting Started class and outside the scope of the CZcams content. The CZcams video is about macro variables and macro functions. You are trying to use macro variables and %SYSFUNC inside a macro program and one of the issues is that your usage is not appropriate for a DATA step. This is the kind of technique you would learn by taking the Coursera class on Macro Language processing or our Macro Language 1 class.
      We recommend that you start with a working SAS program before you introduce macro variables and macro logic into the code. One of the issues in your code is that you cannot use a SAS variable in an %IF statement. Your reference to &PRICE, which will resolve to MSRP (the variable), will NOT work. Your attempt to create a data set variable, CAR, in a %IF statement also will not work. You need to understand the difference between macro compile-time and macro variable resolution and macro execution time. My suggestion is that you rewrite your code, not inside a Macro program, and get it working with a regular IF statement first before you put any macro code in the program. There are a lot of links under the See More section on CZcams that will help you.
      Let us know if this helps!

    • @prasannadillikar7760
      @prasannadillikar7760 Před 3 lety

      @@SASUsers Thanks for clearing my doubts and I will definitely work on what has been suggested by you.
      Thank you

    • @SASUsers
      @SASUsers  Před 3 lety

      You are so welcome!

    • @staceysyphus8238
      @staceysyphus8238 Před 3 lety

      Hi Prasanna - Try this code:
      %MACRO PRAC(PRICE=MSRP,TYPE=SMALL LARGE HEAVY SIXWHEELS);
      DATA SAMPLE;
      SET SASHELP.CARS(KEEP=&PRICE);
      length car $ 8;
      IF 10280.00

  • @harshpratap2920
    @harshpratap2920 Před 9 měsíci

    can you plzz answer my one question that what is future of sas ,Is it replace by AI and we lost our jobs. (mostly in clinical sas industry).

    • @SASUsers
      @SASUsers  Před 9 měsíci

      Hi, there! This article should be helpful: 2.sas.com/6050uEFtC

  • @aboveduality1300
    @aboveduality1300 Před 2 lety

    Kamala? 😯

  • @taipagibonhuchu2256
    @taipagibonhuchu2256 Před 3 lety

    beautiful woman

  • @danielb1877
    @danielb1877 Před rokem

    Good video. But that's not "under $30,000"... it's less than or equal to $30k :)

    • @SASUsers
      @SASUsers  Před rokem

      Thanks for your feedback! We will be sure to share with the presenter.

    • @SASUsers
      @SASUsers  Před rokem

      Hi:
      Yes, you're correct, Technically, the comparison selecting front wheel drive cars that are "less than or equal to" 30000. So a more accurate title for the TITLE2 statement would have been "Front Wheel Drive Cars with a Manufacturer's Selected Retail Price that is Less Than or Equal to $30000". However, that title is somewhat verbose probably would not have looked as neat in the generated graph. The shortened title of "Front Wheel Drive Cars under $30000" manages to convey the purpose of the report. And, if you examine the data in 2.sas.com/6054MMACk, you would see that there are not any cars with front wheel drive and a price of exactly $30,000. For front wheel drive cars in that subset from the WHERE, the minimum value is 10280.00 and the maximum value is 29995.00, which is under 30000. So, given the actual data in 2.sas.com/6054MMACk, the TITLE2 statement accurately describes the subset that was selected by the WHERE and used in the SGPLOT and PROC MEANS.