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;
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
The %str() function call misses the closing bracket.
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
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.
@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.
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:
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;
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 25. 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.