DATA Step, Macro, Functions and more

How to assign length for n number of character variables

Reply
Contributor
Posts: 52

How to assign length for n number of character variables

Hi,

I have n number of character variables I want to assign length for all variables.Below is the peace of data.

Data have;

infile datalines missover;

input name$ surname$ id$;

datalines;

dishant parikh 101

dishantparikh makwana 10001

piyushpatel shah 10000000

;

Run;

Above data is example of original data. I want assign length for all variables with specifying variable names because there are around 300 character variables.

Your help will appreciate. Thanks in advance

Regards,

Dishant

Contributor
Posts: 52

Re: How to assign length for n number of character variables

Hi,

Sorry For Above Line where I mentioned With Instead Of without.So my concern is I want assign length without specifying variable names.

Respected Advisor
Posts: 3,777

Re: How to assign length for n number of character variables

When working with large number of variables like this you will want to treat these names and other meta data (length type informat format label) like data.  Get the names and other meta data into a SAS data set then you can "program" the meta data into the statements you need to define them as variables.

Use the meta data data to generate ATTRIB statements into a file that you can %INC.

Super User
Super User
Posts: 7,413

Re: How to assign length for n number of character variables

Generate the necessary:

Data have;
infile datalines missover;
input name$ surname$ id$;
datalines;
dishant parikh 101
dishantparikh makwana 10001
piyushpatel shah 10000000
;
Run;

data _null_;
  set sashelp.vcolumn (where=(libname="WORK" and memname="HAVE")) end=last;
  if _n_=1 then call execute('proc sql;
                                create table WANT as
                                select  '||strip(name)||' length=300');
  else call execute(','||strip(name)||' length=300');
  if last then call execute('   from HAVE;
                              quit;');
run;

Super User
Posts: 6,963

Re: How to assign length for n number of character variables

You can use this:

length var1-varX $N;

X being the number of variables and N the desired length.

If the variables need to have different lengths/names, you will need to use macro programming or building the data step in a preceding data step using call execute.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 52

Re: How to assign length for n number of character variables

HI All,

Sorry for late replay was unable to replay due to out of station.

Thanks to all for giving your valuable time its takes me new level of thinking.

Specially thanks to RW9 and data_null_ .Those solution inspired me and help me to solve my problem.

Ask a Question
Discussion stats
  • 5 replies
  • 308 views
  • 6 likes
  • 4 in conversation