BookmarkSubscribeRSS Feed
AnuragRankja
Fluorite | Level 6
I am new user of SAS, i have completed my pg1 base SAS course few days ago, Recently i am practicing my course notes where i am facing difficulty in lesson 4, page 167 in English pg1 pdf.

In challenge part i have to use macro variables to change values from mammals to birds using macro variables and have to generate freq report.

%let Category=Bird

data &Category;
set pg1.np_species;

now i want to know that why we use "&Category " in data step?

data step simply create output tables, so in general whatever name we want to give to the table we write it in data step but here we are using different technique can't we use "data bird;" in data step to create table named bird it self instead of this macro variables in data step?

If we can't use "data bird;" so what is the reason?
3 REPLIES 3
Astounding
PROC Star

You could use:

 

data bird;

 

However, there are a couple of advantages to the suggested solution.

 

Presumably, the coding refers to &Category in more than one place.  So you save one item (the data set name) you would otherwise want to change by switching to:

 

data &Category;

 

The data set name will have the accurate spelling.  You will never forget to change the name (thus overwriting some other data set).

 

These are small issues, but reflect an advantage to using macro language.

CurtisMackWSIPP
Lapis Lazuli | Level 10

Your question shows that you are understanding macros.  There is no reason to do what is done in this example.  The macro language is a language used to write SAS code.  They are just showing you that the name of the dataset can be changed just like anything else in the syntax. 

Reeza
Super User

In challenge part i have to use macro variables to change values from mammals to birds using macro variables and have to generate freq report.

 

I think you're thinking about this too much in absolutes. Remember these are all examples, at a basic level and are not absolute you must do it this way examples. This is designed to illustrate how you can dynamically change the code in multiple places without changing things manually. Rather than "I have to use macro variables" it should be thought of as "The question asks me to use macro variables to change my proc freq report automatically". This is one way to accomplish this, there are others. 

 

If you want to know why something is the way it is, try doing it without it and see what happens and compare them. 

If you had used "data bird" in your code instead of data &category, each time you run it what will happen?

Try doing it for Mammals now? And then Reptiles (or whatever category you have)?

It will always write to a data set called Bird which will overwrite your old information. This type of approach allows you to modify your code in one place, rather than multiple places and tends to be less error prone as there's less room for someone to forget to change it in N locations. Your example likely only has one or two references, but what happens if you were using that variable in 50 different places in your code? Would you want to manually change that in each place or just once at the top of your code?

 


@AnuragRankja wrote:

data step simply create output tables

That's also a vast simplification of what a data step does. It does merges, can generate text files and can also not generate any output. It's used for a lot of wrangling in general.

 

Spoiler

@AnuragRankja wrote:
I am new user of SAS, i have completed my pg1 base SAS course few days ago, Recently i am practicing my course notes where i am facing difficulty in lesson 4, page 167 in English pg1 pdf.

In challenge part i have to use macro variables to change values from mammals to birds using macro variables and have to generate freq report.

%let Category=Bird

data &Category;
set pg1.np_species;

now i want to know that why we use "&Category " in data step?

data step simply create output tables, so in general whatever name we want to give to the table we write it in data step but here we are using different technique can't we use "data bird;" in data step to create table named bird it self instead of this macro variables in data step?

If we can't use "data bird;" so what is the reason?

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 835 views
  • 4 likes
  • 4 in conversation