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

Is there any way to compress blank spaces of variable names of all variables without listing the variables individually?


1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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?



 

View solution in original post

11 REPLIES 11
Reeza
Super User

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?



 

ballardw
Super User

@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?

Yegen
Pyrite | Level 9

Perfect thank you. 

Yegen
Pyrite | Level 9

@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;



Yegen
Pyrite | Level 9

@Reeza and @ballardw I have attached a few figures that might provide some hints with the error I am getting:

  • I am sorting the data by using the filter. 

Screen Shot 2018-10-05 at 6.27.55 PM.png

  •  I am trying to edit the filter 

Screen Shot 2018-10-05 at 6.28.14 PM.png

  • Now I am just re-running the same filter that SAS Studio gave me, but I am getting an error.  

Screen Shot 2018-10-05 at 6.28.27 PM.png

 

Yegen
Pyrite | Level 9

Here is the data description: 

Screen Shot 2018-10-05 at 6.31.48 PM.png

 

 

 

ballardw
Super User

@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.

Yegen
Pyrite | Level 9

@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
       

 

Yegen
Pyrite | Level 9

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? 

Reeza
Super User

It’s usually not random, it’s likely in the excel file in some form. 

Yegen
Pyrite | Level 9

That makes sense, thanks @Reeza

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 11 replies
  • 2216 views
  • 7 likes
  • 3 in conversation