Desktop productivity for business analysts and programmers

asking the experts /compress function

Accepted Solution Solved
Reply
Highlighted
Frequent Contributor
Posts: 139
Accepted Solution

asking the experts /compress function

 

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;


Accepted Solutions
Solution
‎03-06-2018 08:34 AM
Super User
Super User
Posts: 9,599

Re: asking the experts /compress function

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


All Replies
Super User
Posts: 10,278

Re: asking the experts /compress function

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎03-06-2018 08:34 AM
Super User
Super User
Posts: 9,599

Re: asking the experts /compress function

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!).

Frequent Contributor
Posts: 139

Re: asking the experts /compress function

I agree with both you. Thanks for refering to implicit conversion.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 132 views
  • 2 likes
  • 3 in conversation