BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tianerhu
Pyrite | Level 9

 

Complete the code below to reference a data set named data_in that is contained in the library cert. You must use the macro variable defined in the program in place of the data set name.

 

 

%let dsref= cert.data_in; libname cert "C:\workshop\cert";

data work.data_out; set ; run; 

 

the correct answer : &dsref , why ?

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Macro variables are just a text substitution mechanism. When you execute SAS code, the value of the macro variable (a text string) is substituted into SAS code (where the macro variable was in the code), and this substitution of macro variable value into the code MUST produce valid working legal SAS code.

 

So, can you spot the error in this code, after the macro variable value is substituted into the code? Is this valid working legal SAS code?

 

data work.data_out;
    set "cert.data_in";
run;

If you had typed the above without a macro variable, will it work? Will it run? What is the problem? If it doesn't work without macro variables, then doing the same thing with macro variables also will not work.


What about this code? Is it valid legal working SAS code? Will it run?

 

data work.data_out;
    set cert.data_in;
run;

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Macro variables are just a text substitution mechanism. When you execute SAS code, the value of the macro variable (a text string) is substituted into SAS code (where the macro variable was in the code), and this substitution of macro variable value into the code MUST produce valid working legal SAS code.

 

So, can you spot the error in this code, after the macro variable value is substituted into the code? Is this valid working legal SAS code?

 

data work.data_out;
    set "cert.data_in";
run;

If you had typed the above without a macro variable, will it work? Will it run? What is the problem? If it doesn't work without macro variables, then doing the same thing with macro variables also will not work.


What about this code? Is it valid legal working SAS code? Will it run?

 

data work.data_out;
    set cert.data_in;
run;

 

--
Paige Miller
ballardw
Super User

@tianerhu wrote:

 

Complete the code below to reference a data set named data_in that is contained in the library cert. You must use the macro variable defined in the program in place of the data set name.

 

 

%let dsref= cert.data_in; libname cert "C:\workshop\cert";

data work.data_out; set ; run; 

 

the correct answer : &dsref , why ?


Consider that the basic way to write a set statement is libname.datasetname. Suppose I want to use the SAS supplied data set Sashelp.class on a set statement. The data step would look like:

 

Data work.want;
   set sashelp.class;
<any other programming statements>
run;

If I place quotes around the data set name like "sashelp.class" this is what happens:

213  Data work.want;
214     set "sashelp.class";
ERROR: Extension for physical file name "sashelp.class" does not correspond to a valid member
       type.
215  /*any other programming statements*/
216  run;

The double quotes are needed when I want the value treated as a string value. A statement to assign a value to a variable perhaps like:

Name='John';

If John was stored in a macro variable the statement would be

Name="&Macrovar";

because the use of the variable as a value requires quotes.

If I want to create a new variable in the data set using a macro variable to hold the name it would not be in quotes because for most uses variable names aren't in quotes.

%let newvar=Boyname;
Data work.want;
   set sashelp.class;
   if sex='M' then &newvar.=Name;
run;

The rule depends on what the value is used for. If the use requires quotes such as literal value or an external file name then the macro variable is in double quotes so it will resolve but the result is still quoted.

 

Moral of the story is get the code working without macro variables. Then look at the code an what you replace gets quoted or not based on usage.

tianerhu
Pyrite | Level 9

Thank you for your help.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1109 views
  • 0 likes
  • 3 in conversation