Hello,
I'm trying to learn how to use prompts in SAS:
I have prompt set up as 'prompt_1' which can either have value of Year1 or Yearly. I have a couple of proc sql and data set statements that need to run only if the prompt_1 value is Year1. Please find the code, it gives no errors, but I get no output either. Please suggest what am I doing wrong.
data _null_;
%put &prompt_1.;
prompt_1 = "&prompt_1.";
run;
%macro determinecall(prompt_1);
%if %upcase(prompt_1) = 'YEAR1' %then %GO;
%mend determinecall;
%determinecall(prompt_1);
%macro GO;
proc printto log= "/sasuserhome/test/test.log";
run;
proc sql;
create table paylist
(IdNum char(4),
Gender char(1),
Jobcode char(3),
insert into paylist
values('1639','F','TA1')
values('1065','M','ME3');
select *
from paylist;
quit;
data alpha;
infile datalines;
input a b c;
datalines;
1 5 10
0 8 7
1 4 6
;
run;
proc print data= alpha;
run;
%mend;
%upcase(prompt_1) = YEAR1
Try removing the quotes.
@astha8882000 wrote:
Hello,
I'm trying to learn how to use prompts in SAS:
I have prompt set up as 'prompt_1' which can either have value of Year1 or Yearly. I have a couple of proc sql and data set statements that need to run only if the prompt_1 value is Year1. Please find the code, it gives no errors, but I get no output either. Please suggest what am I doing wrong.
data _null_; %put &prompt_1.; prompt_1 = "&prompt_1."; run; %macro determinecall(prompt_1); %if %upcase(prompt_1) = 'YEAR1' %then %GO; %mend determinecall; %determinecall(prompt_1); %macro GO; proc printto log= "/sasuserhome/test/test.log"; run; proc sql; create table paylist (IdNum char(4), Gender char(1), Jobcode char(3), insert into paylist values('1639','F','TA1') values('1065','M','ME3'); select * from paylist; quit; data alpha; infile datalines; input a b c; datalines; 1 5 10 0 8 7 1 4 6 ; run; proc print data= alpha; run; %mend;
Is this the code you would actually want to use? I'm not sure you can create tables that way with PROC SQL, I know you can't use CARDS/DATALINES in a macro.
Have you checked the value of your parameters using %PUT statements?
Check the condition execution with %PUT?
Use MPRINT/SYMBOLGEN to see what's happening.
@astha8882000 wrote:
As of now I'm just trying to identify why my macro GO is not being called. Had my macro been called, I'm pretty sure proc sql statements would have caused errors.
Could it be because you trying to run the first macro (the one that calls %GO()) before you actually defined the GO macro?
This works:
ods listing;
%macro TEST_MACRO;
proc print data= sashelp.class;
run;
%mend;
%macro determinecall(prompt_1);
%if %upcase(&prompt_1) = YEAR1 %then %test_macro;
%mend determinecall;
%let prompt_1 = YEAR1;
%determinecall(&prompt_1);
%let prompt_1 = YEAR2;
%determinecall(&prompt_1);
Now, let's go through your code, see the comments below.
Note that GO is also a reserved word, so you shouldn't use that as a macro name.
data _null_;
%put &prompt_1.;
prompt_1 = "&prompt_1."; *this line doesn't do anything;
run;
%macro determinecall(prompt_1); *here you reference a new value prompt_1 but it's local to the macro, not your macro variable;
*In this line you forgot the & to resolve the macro variable and you need to remove quotes;
%if %upcase(prompt_1) = 'YEAR1' %then %GO; *%GO macro has not be defined yet, not sure if that's an issue, but logically it seems to be;
%mend determinecall;
*calling it here without an actual value since no ampersand;
%determinecall(prompt_1);
%macro GO;
proc printto log= "/sasuserhome/test/test.log";
run;
*not sure SAS macros ccan create tables like this;
proc sql;
create table paylist
(IdNum char(4),
Gender char(1),
Jobcode char(3),
insert into paylist
values('1639','F','TA1')
values('1065','M','ME3');
select *
from paylist;
quit;
*this is definitely invalid in a macro;
*datalines dont run;
data alpha;
infile datalines;
input a b c;
datalines;
1 5 10
0 8 7
1 4 6
;
run;
proc print data= alpha;
run;
%mend;
First check what is in the macro variable. You could use this macro statement.
%put &=prompt_1 ;
Or if you want to use data step code then something like this. Added the $QUOTE format will put quotes around the value in the log to make it easier to see if the value had leading spaces.
data _null_;
length prompt_1 $100 ;
prompt_1 = symget('prompt_1');
put prompt_1 = :$quote. ;
run;
In general to test a value in macro code you do not want to add quotes since the quotes will be part of the value.
So if PROMPT_1 has Year1 in it then you need to test in macro logic like this:
%if &prompt_1 = Year1 %then ....
Or like this:
%if "&prompt_1" = "Year1" %then ....
You might need to make sure the case is right.
%if %qupcase(&prompt_1) = YEAR1 %then ....
coz like @Tom says your macro GO is not yet compiled for execution when you execute %determinecall
Also you can't have datalines code in a macro definition
Then your sql syntax needs to be corrected as I posted previously
proc sql;
create table paylist
(IdNum char(4),
Gender char(1),
Jobcode char(3)) /*notice the correction here*/
;
insert into paylist
values('1639','F','TA1')
values('1065','M','ME3');
select *
from paylist;
quit;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: