BookmarkSubscribeRSS Feed
5 REPLIES 5
Kurt_Bremser
Super User

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.

Sabharish
Fluorite | Level 6

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.

 

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

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 844 views
  • 3 likes
  • 3 in conversation