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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

6 REPLIES 6
art297
Opal | Level 21

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

 

Astounding
PROC Star

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.

Kurt_Bremser
Super User

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

Cynthia_sas
SAS Super FREQ

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1135 views
  • 7 likes
  • 6 in conversation