DATA Step, Macro, Functions and more

SAS macro Function %STR

Accepted Solution Solved
Reply
Contributor LL5
Contributor
Posts: 44
Accepted Solution

SAS macro Function %STR

I am trying to put the age into each age group by using %str function. However, I have syntax error shown as below.

 

The second question is it seems after I put the semi-colon after each if-then statement, the color for the next else statement becomes red. However, if i take away the semi-colon, the whole program didn't work.

 

Does anyone have any clue about this? Thanks.

 

1 ;*';*";*/;quit;run;

______________

49

NOTE 49-169: The meaning of an identifier after a quoted string might change in a future SAS release. Inserting white space

between a quoted string and the succeeding identifier is recommended.

 

 

%let age4=%str(

if age < 20 then group = " <20 ";

else if age < 35 then group = " 20-34 ";

else if age < 45 then group = " 35-44 ";

else group = " 45+ " ;

 

data test;

input age @@;

&age4;

datalines;

19 50 10 34 44 15 30

;

run;

proc print data=test;

run;


Accepted Solutions
Solution
‎03-13-2017 10:30 AM
PROC Star
Posts: 7,480

Re: SAS macro Function %STR

You're missing a closing parenthesis and semi-colon. Try:

 

%let age4=%str(
if age < 20 then group = " <20 ";
else if age < 35 then group = " 20-34 ";
else if age < 45 then group = " 35-44 ";
else group = " 45+ " ;);
 
data test;
  input age @@;
  &age4;
  datalines;
19 50 10 34 44 15 30
;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Super User
Posts: 7,809

Re: SAS macro Function %STR

The %str() function call misses the closing bracket.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎03-13-2017 10:30 AM
PROC Star
Posts: 7,480

Re: SAS macro Function %STR

You're missing a closing parenthesis and semi-colon. Try:

 

%let age4=%str(
if age < 20 then group = " <20 ";
else if age < 35 then group = " 20-34 ";
else if age < 45 then group = " 35-44 ";
else group = " 45+ " ;);
 
data test;
  input age @@;
  &age4;
  datalines;
19 50 10 34 44 15 30
;

Art, CEO, AnalystFinder.com

 

Super User
Posts: 5,513

Re: SAS macro Function %STR

While you are missing that closing parenthesis in your post, I think it's likely that you included it in your actual code.  Otherwise the results would have been different.

 

SAS often uses a character immediately following a closing quote to change the meaning of a character string. 

 

"13MAR2017" ==> character string

"13MAR2017"d ==> numeric SAS date

 

So SAS is just warning  you that you have a character immediately following a closing quote.  You can get rid of those messages by adding a space between the closing quotes and the statement-ending semicolons.

Super User
Posts: 7,809

Re: SAS macro Function %STR

Posted in reply to Astounding

@Astounding: the NOTE 49-169 often happens when the "magic closing string" sent by EG catches anything unbalanced and subsequently creates a quote-immediately-followed-by-something-else situation by itself.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
SAS Super FREQ
Posts: 8,868

Re: SAS macro Function %STR

Hi:

  The %LET statement ends with the first semi-colon and so, you are using both %LET and %STR in a way that is going to cause you problems. Putting an entire code snippet into a macro variable isn't the best idea. Using %INCLUDE for complete code snippets or a small macro program are better choices.

 

  But, I'm not sure why you even want to use a macro variable for your creation of the GROUP variable. You can easily do what you want with a user defined format....no macro needed.

 

cynthia

 

This program and output requires no macro code at all.

proc format;
  value grpf low-13 = ' <14'
             14-15 = '14-15'
	     16-high = '16-high';
run;

data test;
infile datalines dlm=',';
input name $ age  ;
group = put(age,grpf.);
return;
datalines;
Alan,12 
Barb,11
Carl,13
Dave,15
Eliza,15 
Frank,16
Gina,17
Hal,18
Inez,19
John,20
Kay,21
Louise,12 
Mike,14
Norma,14
Oona,15 
;
run;

proc print data=test;
run;

and produces:

with_format.png

Super User
Super User
Posts: 7,970

Re: SAS macro Function %STR

Why are you putting SAS code into a macro variable, that doesn't sound like a good way to proceed.  You could just setup a format for that condition, several other better methods.

proc format;
value agefmt
low-20 = "<20"
21-35 = "20-34";
run;

data test;
input age @@;
format age agefmt.;
datalines;
19 50 10 34 44 15 30
;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 206 views
  • 7 likes
  • 6 in conversation