Is there any way to compress blank spaces of variable names of all variables without listing the variables individually?
You can use _character_ to reference all character variables in an array and compress the spaces. Note that the spaces are determined by the length of the variable, so those spaces will still exist.
array _test(*) $ _character_;
do i=1 to dim(_test);
_test(i) = compress(_test(i));
end;
@Yegen wrote:
Is there any way to compress blank spaces of variable names of all variables without listing the variables individually?
You can use _character_ to reference all character variables in an array and compress the spaces. Note that the spaces are determined by the length of the variable, so those spaces will still exist.
array _test(*) $ _character_;
do i=1 to dim(_test);
_test(i) = compress(_test(i));
end;
@Yegen wrote:
Is there any way to compress blank spaces of variable names of all variables without listing the variables individually?
@Yegen wrote:
Is there any way to compress blank spaces of variable names of all variables without listing the variables individually?
Spaces in variable names or values of variables?
Perfect thank you.
@ballardw spaces in variable names.
The solution proposed by @Reeza works well with character variables, but I am still facing issues with date variables. In particular, I have imported excel files (using a macro) and for some reason blank spaces were added to the variable names. I cannot identify the number of spaces even if I use a code as follows:
proc contents
data = work.temp_data
noprint
out = data_info
(keep = name varnum);
run;
Here is the data description:
@Yegen wrote:
@ballardw spaces in variable names.
The solution proposed by @Reeza works well with character variables, but I am still facing issues with date variables. In particular, I have imported excel files (using a macro) and for some reason blank spaces were added to the variable names. I cannot identify the number of spaces even if I use a code as follows:proc contents data = work.temp_data noprint out = data_info (keep = name varnum); run;
It sounds like you may have imported the data with the option validvarname set to "any".
Run this code to see if that is the case:
proc options option=validvarname; run;
If the LOG shows a result similar to
VALIDVARNAME=ANY Specifies the rules for valid SAS variable names that can be created and processed during a SAS session.
then you can likely "fix" the problem by setting
option validvarname=V7;
and reimporting.
@ballardw, this is the macro I was using to import the excel files:
%macro merging;
%do i=1 %to 8;
PROC IMPORT DATAFILE="/home/institution/user/SDC_M_A/sdc_oct5_&i..xlsx"
DBMS=XLSX
OUT=WORK.merging&i;
GETNAMES=yes;
RUN;
%end;
%mend;
%merging;
If I use your code, this is the log file I am getting:
90
91 proc options option=validvarname;
92 run;
SAS (r) Proprietary Software Release 9.4 TS1M5
VALIDVARNAME=ANY Specifies the rules for valid SAS variable names that can be
created and processed during a SAS session.
NOTE: PROCEDURE OPTIONS used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
You are 100% right, @ballardw. When I added the
option validvarname=V7;
statement in the macro the issue was fixed.
I assume the import function was just adding random blank spaces when you don't specify the validvarname?
It’s usually not random, it’s likely in the excel file in some form.
That makes sense, thanks @Reeza
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 25. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.