BookmarkSubscribeRSS Feed
astha8882000
Obsidian | Level 7

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;

 

 

14 REPLIES 14
Reeza
Super User
%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;

 

 



 

astha8882000
Obsidian | Level 7
Tried doing that too, still no output.
Reeza
Super User

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
Obsidian | Level 7
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.
Tom
Super User Tom
Super User

@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?

astha8882000
Obsidian | Level 7
I moved the GO macro above the determinecall macro, still no output.
Reeza
Super User

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.

http://support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/default/viewer.htm#p0y43hj7lzhq1gn1r...

 

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;
astha8882000
Obsidian | Level 7
I made the changes like you suggested, and I think it works. I'm going to try on another code which is a huge list of proc sqls and data _null statements and see if it works for that too. Thank you so much for your help!!
Tom
Super User Tom
Super User

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 ....
astha8882000
Obsidian | Level 7
I checked the value of prompt_1 and it does contain Year1

I also updated the if statement to %if &prompt_1 = Year1 %then

But I still get no output, not sure why GO is not being called.
novinosrin
Tourmaline | Level 20

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

astha8882000
Obsidian | Level 7
I moved the GO macro definition before the determinecall macro definition, but that didn't help either.
For the datalines, once my macro gets called, I'm pretty sure I will get an error for that.
novinosrin
Tourmaline | Level 20


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;

astha8882000
Obsidian | Level 7
made the change, thank you for correcting.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 1864 views
  • 0 likes
  • 4 in conversation