DATA Step, Macro, Functions and more

Common SAS issue with lists

Reply
Occasional Contributor
Posts: 18

Common SAS issue with lists

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. 

Trusted Advisor
Posts: 1,931

Re: Common SAS issue with lists

Posted in reply to sasnewbie12

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
Super User
Posts: 19,822

Re: Common SAS issue with lists

Posted in reply to sasnewbie12

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. 

Super User
Super User
Posts: 7,060

Re: Common SAS issue with lists

Posted in reply to sasnewbie12

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.

image.png

image.png

 

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
;

 

Super User
Posts: 7,809

Re: Common SAS issue with lists

Posted in reply to sasnewbie12

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,060

Re: Common SAS issue with lists

Posted in reply to KurtBremser

KurtBremser 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;
Super User
Posts: 7,809

Re: Common SAS issue with lists


Tom wrote:

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 4,927

Re: Common SAS issue with lists

Posted in reply to KurtBremser

KurtBremser wrote:

Rethink your data structure.


I think this is wise advice.

PG
Occasional Contributor
Posts: 18

Re: Common SAS issue with lists

Posted in reply to sasnewbie12

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.

 

Super User
Super User
Posts: 7,060

Re: Common SAS issue with lists

Posted in reply to sasnewbie12

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;
Trusted Advisor
Posts: 1,571

Re: Common SAS issue with lists

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

Ask a Question
Discussion stats
  • 10 replies
  • 178 views
  • 11 likes
  • 7 in conversation