BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

Hello,

 

I have checked on the web and I have found many way to check if a variable exist but how do we create new variable with specific format.

 

What's the best way ?

I have tried the length statement in both case when the variable exist and when the variable does not exist and I don't see any error or warning into the log. 

But is there a better way to do that, ie. check if the variable exist and if not create it with a specific format.  

 

The question is how to do that 

 

data class1 (drop=sex);
set sashelp.class;
run;

data class2;
set sashelp.class;
run;

data test;
length sex $ 1.;
set class1;
run;

data test2;
length sex $ 1.;
set class2;
run;

data test3;
set class2;
run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@alepage wrote:
I have tried the varexist macro function and it does not seems to works.

Provide example code where it does not work.

 

Here is example of how to retrieve and compile the macro and then use it to detect the variable type.

filename varexist url "https://raw.githubusercontent.com/sasutils/macros/master/varexist.sas";
%include varexist;
%put NAME %varexist(sashelp.class,name,type);
%put AGE  %varexist(sashelp.class,age,type);
%put XXX  %varexist(sashelp.class,xxx,type);

Results:

695  %put NAME %varexist(sashelp.class,name,type);
NAME C
696  %put AGE  %varexist(sashelp.class,age,type);
AGE  N
697  %put XXX  %varexist(sashelp.class,xxx,type);
XXX  0

View solution in original post

14 REPLIES 14
Reeza
Super User
Format, or type (character/numeric) or length? You seem to be using the term interchangeably but they're all specific terms for SAS programming. I suspect you want type.

What happens if the variable exists but isn't the right type?
alepage
Barite | Level 11
Good afternoon,

Yes you are right. I should not use the term with specific format. It is a bad translation from French to English. In that case, I refer to type effectively
alepage
Barite | Level 11
Hello Reeza,
You are right. I should not use the term Format for what I have in mind. It is a bad translation from French to English. And in SAS format is something else. What I refer to is the type ex: character or numeric and the length as well.

Moreover, again, you are right, what happen if it is the variable exist but if the type or the length is not good.
AlanC
Barite | Level 11
Always, ALWAYS, start a data step with an attrib statement describing each variable. Do not use the old means of handling it like length function. The attrib will create the variable for you exactly as you want it.
https://github.com/savian-net
alepage
Barite | Level 11
Thank you very much for this precision. What's about the best way to check if a variable exist and if not how do we integrate the attrib function information into the dataset?
SASKiwi
PROC Star

You can't conditionally execute an ATTRIB or LENGTH statement as it is not an executable statement. It is good practice if you want to ensure that a variable always has a required type and length to define it in an ATTRIB statement regardless of whether it exists in your input datasets or not. 

Tom
Super User Tom
Super User

@AlanC wrote:
Always, ALWAYS, start a data step with an attrib statement describing each variable. Do not use the old means of handling it like length function. The attrib will create the variable for you exactly as you want it.

The second sentence does not make any sense.  There is no difference in how the variable is defined (its type and its storage length) that is done differently with an ATTRIB statement than with the LENGTH statement.  Perhaps you meant to say that you personally prefer to use an ATTRIB statement with the LENGTH= option to define the variables instead of just using a LENGTH statement?

AlanC
Barite | Level 11
The attrib, introduced later, handles all aspects for the pdv including length, format, informat, and label. Hope that clears it up.
https://github.com/savian-net
Patrick
Opal | Level 21

@AlanC The attrib statement will only assign a format if you explicitly use the format option so it's really only another syntax option in place of using the length, format, informat and label statements.

AlanC
Barite | Level 11

Yes, I know. I think it was introduced in v6 but it simplified the variable creation a lot and initializes the PDV. It doesn't do anything different but it puts the variable info in one place and is easier to read.

 

Here is statement on my Tips/Tricks on GitHub: savian-net/SasTipsTricks: Tips and tricks learned over 20+ years as a SAS/Microsoft consultant (gith...

 

When I was a SAS consultant, I left the client's with a variation of that Tips/Tricks document...at every client. I 'think' it was best practice to use ATTRIB in SAS Consulting but not sure (too many years). Here is sample code:

 

libname temp 'x:\temp';

data temp.temp (drop=i);
   attrib AA1 label="Var1"
          AA2 label="Var2"
          AA3 label="Var3" length=$10 format=$7. informat=$9.
          AA4 label="Var4" length=$10
          AA5 label="Var5" length=$10
          AA6 label="Var6" length=$10
          AA7 label="Var7" length=$10
          AA8 label="Var8" length=$10
          AA9 label="Var9"
		  ;
   do i = 1 to 10;
      aa1 = 1 ;
	  aa2 = 2;
      aa3 = "Test_" || put(i,z4.);
	  aa4 = "Test2" ;
	  aa5 = "Test3" ;
	  aa6 = "Test4" ;
	  aa7 = "Test5" ;
	  aa8 = "Test6" ;
	  aa9 = 12 ;
 	  output ;
   end;
run;

 

 

https://github.com/savian-net
Tom
Super User Tom
Super User

So FORMAT has a specific meaning in SAS and it is not what your code is doing.  Your code is defining the variable.  It is not attaching a format to specify the way that you want SAS to display the values.  SAS has only two types of variables, floating point numbers or fixed length character strings.  So setting the LENGTH will define for SAS which type it is, and also how much storage space to use for it when writing the dataset to disk.

 

The reason why it might be useful to test if the variable already exists or not is to avoid the error that will happen if you try to define it with a different type.  For example try this program:

498  data class1;
499    set sashelp.class;
500    length sex 8 age $3 ;
ERROR: Numeric length cannot be used with character variable Sex.
ERROR: Character length cannot be used with numeric variable Age.
501  run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.CLASS1 may be incomplete.  When this step was stopped there were 0 observations and 5 variables.

Now if you use a macro like %varexist() that can check if the variable exists and also its type then you can use macro logic to generate the proper code.

 

So if SEX is supposed to be character and it is numeric you might do something like:

%if %varexist(class,sex,type)=N %then %do;
data want;
  set class(rename=(sex=sexn));
  sex=put(sexn,1.);
run;
%end;

And if SEX is supposed to be numeric and it is character you might do something like:

%if %varexist(class,sex,type)=C %then %do;
data want;
  set class(rename=(sex=sexc));
  sex=input(sexc,32.);
run;
%end;

 

alepage
Barite | Level 11
I have tried the varexist macro function and it does not seems to works.
Tom
Super User Tom
Super User

@alepage wrote:
I have tried the varexist macro function and it does not seems to works.

Provide example code where it does not work.

 

Here is example of how to retrieve and compile the macro and then use it to detect the variable type.

filename varexist url "https://raw.githubusercontent.com/sasutils/macros/master/varexist.sas";
%include varexist;
%put NAME %varexist(sashelp.class,name,type);
%put AGE  %varexist(sashelp.class,age,type);
%put XXX  %varexist(sashelp.class,xxx,type);

Results:

695  %put NAME %varexist(sashelp.class,name,type);
NAME C
696  %put AGE  %varexist(sashelp.class,age,type);
AGE  N
697  %put XXX  %varexist(sashelp.class,xxx,type);
XXX  0

alepage
Barite | Level 11
Thank you very much

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 14 replies
  • 1303 views
  • 1 like
  • 6 in conversation