BookmarkSubscribeRSS Feed
sasnewbie12
Obsidian | Level 7

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. 

10 REPLIES 10
PaigeMiller
Diamond | Level 26

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
--
Paige Miller
Reeza
Super User

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. 

Tom
Super User Tom
Super User

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
;

 

Kurt_Bremser
Super User

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.

Tom
Super User Tom
Super User

@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;
Kurt_Bremser
Super User

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

PGStats
Opal | Level 21

KurtBremser wrote:

Rethink your data structure.


I think this is wise advice.

PG
sasnewbie12
Obsidian | Level 7

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.

 

Tom
Super User Tom
Super User

@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;
Shmuel
Garnet | Level 18

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1069 views
  • 11 likes
  • 7 in conversation