@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;
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.
Ah okay, so I read it like:
Where [...] the character->numeric converted quarter-variable contains the digits of &quartal2.?
quarter-variable contains one of the digits of &quartal2.?
@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 ...
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: