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

 

Hello,

 

I found an old lock program for the company I work for (see below). They use the compression function with a numeric variable.

 

I thought the compression function should only be used with character variables. Maybe I'm wrong!

 

Is it a particular use of which I am not informed?

Please note that week, year and date are numerical variables

 

data _null_ ;
set commun.dates (where = (date = date()));

if day = 2 then do;
call symput ("Monday",1);
call symput ("week",compress(week-1));
call symput ("current_week",compress(week));
call symput ("dt_Monday",date);
end;
else do;
call symput ("Monday",0);
call symput ("week",compress(week));
call symput ("current_week",compress(week));
end;

call symput ("year",compress(year));
run;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There is in SAS something called implicit conversion.  What this means is that if a variable type does not conform to the given required type, then SAS will try to convert the type from to the other, so numeric to text and vice versa.  In your case SAS is implicitly converting the number to a string and then performing the action. 

In my opinion this is quite bad practice, all conversions should be open and precise.  First it avoids questions like this, but also the conversion may not be to something you actually want.  Always use explicit conversions using put() and input().  Macro varaibles are always text hence no conversion needed there (and one reason why not to store data in them!).

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

SAS will automatically convert numbers to character whenever numeric values are used where character is expected. The compress() function is needed to get rid of the blanks resulting from such automatic conversions.

 

I personally do not tolerate automatic conversions in my programs.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

There is in SAS something called implicit conversion.  What this means is that if a variable type does not conform to the given required type, then SAS will try to convert the type from to the other, so numeric to text and vice versa.  In your case SAS is implicitly converting the number to a string and then performing the action. 

In my opinion this is quite bad practice, all conversions should be open and precise.  First it avoids questions like this, but also the conversion may not be to something you actually want.  Always use explicit conversions using put() and input().  Macro varaibles are always text hence no conversion needed there (and one reason why not to store data in them!).

alepage
Barite | Level 11
I agree with both you. Thanks for refering to implicit conversion.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 1023 views
  • 2 likes
  • 3 in conversation