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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 21 replies
  • 1794 views
  • 12 likes
  • 4 in conversation