BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kurt_Bremser
Super User

@Konkordanz wrote:

Thx for your replys. In general, it works without these quotes. But: The reason for using the macro variable with quotes is the step #2 (pls see above): After a user entered the quarter, the dataset should be filtered depending of this information:

 

Data want;
set have;
where (Year="&Year2." or Year="&Year1.") and Quarter in(&quarter2.);
run;

With quotes the where command would be for example: "[...] quarter in("1","2","3","4");...If I avoid the quotes, this command wouldnt work. If I had another way for filtering the dataset without the quotes, It would be great. Do you have one?


This is trivial to solve:

%let Quarter2=1,2,3,4;

data _null_; 
call symputx ("quarter_final", ifc(indexc("&Quarter2.","4"),"4","1"));
run;

Data want;
set have;
where (Year="&Year2." or Year="&Year1.") and input(quarter,1.) in(&quarter2.);
run;
Konkordanz
Pyrite | Level 9
Wow, that seems to work. But I doesnt understand why. What is "and input(quarter,1.) in(&quarter2.)" doing? I cant find the answer on the internet...can you explain it or submit a link, where I can learn it?
Kurt_Bremser
Super User

The INPUT function works similar to the INPUT statement. It reads the string, applies the informat to it, and returns the number if the conversion works.

If you had stored your quarters as numbers in the first place, all this would be easier.

Konkordanz
Pyrite | Level 9

Ah okay, so I read it like:

Where [...] the character->numeric converted quarter-variable contains the digits of &quartal2.?

Konkordanz
Pyrite | Level 9
Thanks again!
FreelanceReinh
Jade | Level 19

@Konkordanz wrote:

Ah okay, so I read it like:

Where [...] the character->numeric converted quarter-variable contains the digits of &quartal2.?


If the length of character variable quarter is >1, input(quarter,1.) will read and convert only the first character of the value in quarter. But you can easily specify a longer informat (such as 8. or even 32.) to ensure that, e.g., "  3" (with leading blanks) is recognized as 3 and "123" is not converted to 1. Also, to avoid warning messages in case of non-numeric values in quarter (like "F") you can insert the "?" modifier:

... and input(quarter, ? 32.) in ...

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 21 replies
  • 2762 views
  • 12 likes
  • 4 in conversation