I have a long list of variables up to 5000. I want to include these in a sas proc. However, how can I paste these from word or excel into sas without having a long linear list where i have to scroll down for a long time to see. I would like to have it spread out over multiple rows (without having to manually go in and rearrange it).
In other words, I want to make this:
1
2
3
4
5
6
7
8
into this:
1 2 3 4
5 6 7 8
so it can be easier to manage.
If the variables are numbered sequentially (are they?) then you could use
x1-x5000
If they are not numbered sequentially, then you could use the syntax of the format
first -- last
I personally wouldn't work with either of those approaches. Load your data into a text or Excel file and import them directly into your process.
You can use the TF line command (text flow) in the program editor (not the "enhanced" editor, but the original editor that allows you to type command letters into the line number area). But since they have broken it lately you will need to paste them into the bottom of the program to prevent it from re flowing the whole program.
You could just read them in and write them back to log where you could copy and paste them again.
data _null_;
length name $32 ;
input name @@ ;
file log column=cc ;
if cc > 60 then putlog ;
putlog name @ ;
cards;
name
sex
age
height
weight
;
Keep in mind that the limit for the textual size of a SAS statement is 32767, so handling that many variables might prove impossible.
Rethink your data structure.
@Kurt_Bremser wrote:
Keep in mind that the limit for the textual size of a SAS statement is 32767, so handling that many variables might prove impossible.
Rethink your data structure.
That statement is not true. Variables are limited to 32,767. Macro variables are twice that. Statement as far as I know have no limit.
Try running this code that will create a LABEL statement that is more can 64K characters long.
filename code temp;
data _null_;
length name $32 label $256 ;
file code lrecl=300 ;
put 'label';
do i=1 to 300 ;
name = cats('VAR',i);
label = 'label for ' || name ;
put ' ' name $32. '="' label $256. '"' ;
end;
put ';' ;
run;
data test;
array xx var1-var300 ;
%include code ;
run;
@Tom wrote:
@Kurt_Bremser wrote:
Keep in mind that the limit for the textual size of a SAS statement is 32767, so handling that many variables might prove impossible.
Rethink your data structure.
That statement is not true. Variables are limited to 32,767. Macro variables are twice that. Statement as far as I know have no limit.
Try running this code that will create a LABEL statement that is more can 64K characters long.
filename code temp; data _null_; length name $32 label $256 ; file code lrecl=300 ; put 'label'; do i=1 to 300 ; name = cats('VAR',i); label = 'label for ' || name ; put ' ' name $32. '="' label $256. '"' ; end; put ';' ; run; data test; array xx var1-var300 ; %include code ; run;
You're right. I confused this with the limits of a put statement, which comes into play when proc export tries to write the header for a csv file.
KurtBremser wrote:Rethink your data structure.
I think this is wise advice.
Thanks for your inputs.
Just to clarify, the variable names are unrelated and not in any kind of order. I know that we can do this with a macro or code; is there any way to do this quickly, I find myself coming across this issue frequently; if it's not 5000, I still may have a hundred variables and it doesn't look clean to have a long linear list within my code syntax.
@sasnewbie12 wrote:
Thanks for your inputs.
Just to clarify, the variable names are unrelated and not in any kind of order. I know that we can do this with a macro or code; is there any way to do this quickly, I find myself coming across this issue frequently; if it's not 5000, I still may have a hundred variables and it doesn't look clean to have a long linear list within my code syntax.
If you have the list in a dataset then it is easy to generate the list into a macro variable.
proc sql noprint ;
select name into :varlist separated by ' '
from varlist
;
quit;
proc print data=have ;
var &varlist;
run;
@sasnewbie12, what kind of file contains that list ? Can you save it as a csv file ?
If positive then you can addapt next code to your needs:
filename mylist '...path_and_name.csv';
data temp;
infile mylist dlm=',' truncover;
input var_in <informat.>;
run;
filename list4 '...path_and_name.sas';
data _null_;
set temp;
retain var1-var4; /* addapt to nomber of columns */
array varx $ var1-var4; /* assumed 4 columns */
i = mod(_N_ - 1,4) +1; /* assumed 4 columns */
varx(i) = var_in;
file list4;
line = catx(' ',var1,var2,var3,var4);
if i=4 then put line;
run;
then you can use %INCLUDE LIST4; later in your code.
If some of the variables are numeric and other character type then you may need add format/informat info to the list and to the output file.
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!
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.