DATA Step, Macro, Functions and more

can anyone tell me the difference between declaring and definin a variable in sas?

Reply
Occasional Contributor
Posts: 12

can anyone tell me the difference between declaring and definin a variable in sas?

 
Super User
Posts: 6,962

Re: can anyone tell me the difference between declaring and definin a variable in sas?

Since SAS variables are "declared" implicitly through first use or being present in an input dataset, there is no semantic difference, IMO.

There is a DEFINE statement in proc report used to define report columns.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 12

Re: can anyone tell me the difference between declaring and definin a variable in sas?

Thanks for the information.

 

But, Im asking while writing data step. In which statement the variables are declared and defined in SAS.

 

Eg:

 

Data Test;

         Length empname $ 20; 

         input id empname $;

cards;

1 XXX

2 YYY

3 ZZZ

;

run;

 

Can you explain me in which statement we are decalring and defining the variables in this data set.

 

Thanks in advance.

 

Super User
Posts: 6,962

Re: can anyone tell me the difference between declaring and definin a variable in sas?

data test;
length empname $ 20; /* at this point, the data step compiler adds the variable empname to the PDV, sets its type to character, and reserves 20 bytes for the content; no format, informat or label is being assigned */
input id empname $;
cards;
1 XXX
2 YYY
3 ZZZ
;
run;

So you can see that there really is no point to the question "when is a variable declared", as SAS does not have the concept of a mandatory declaration before a variable can be used. Sometimes the final "definition" of a variable is the result of quite a lot of statements scattered all over the data step. Since some of these statements will set attributes that cannot be changed later, knowledge how the data step handles these statements is mandatory for the experienced (and not-so-experienced) SAS programmer.

Start your exploration here: SAS 9.4 Language Concepts

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 12

Re: can anyone tell me the difference between declaring and definin a variable in sas?

Thanks!

Super User
Super User
Posts: 6,502

Re: can anyone tell me the difference between declaring and definin a variable in sas?

Not sure if those are the right terms to use.  I think a better distinction might be is to think about which variable references can cause SAS to detect that you have referred to a new variable and so add it to the list of variables in the data set (or the "PDV") and which statement actually impact the type,length,format,informat,label and other metadata about the variable.  

 

So some references will only cause SAS to add the variable to PDV.  The most used one is the RETAIN statement. But also a LABEL statement will add the variable without settng the type.

Other references will cause SAS to both add the variable to PDV and also set the type and length.  So using a variable in an assignment statement will do this.  

x=4.5;
y='Fred';

Assigning a format to a variable.  

format dob date9.;

Referencing a dataset will cause SAS to add all of the variables in that dataset, even references that can never actually execute.

if 1=0 then set sashelp.class;

The best way to "define" a variable is to reference an existing dataset that has the variable

data new ;
   set old;

or use a LENGTH or ATTRIB statement to explicitly set the type and length of the variable.

data new ;
  length name $20 age 8 ;

 

There are also statements that can change attributes of the variables, such as the LABEL or FORMAT statement, that can appear multiple times and the last one will "win".  This can be useful to remove unwanted formats from variables. For example SAS has a nasty habit of attaching formats to variables read from external databases or generated by PROC IMPORT.

data want ;
  set ora.table1 ;
  format _all_ ;
  dob = datepart(dob);
  format dob date9. ;
run;
Ask a Question
Discussion stats
  • 5 replies
  • 135 views
  • 3 likes
  • 3 in conversation