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;
@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
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.
@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 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.
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;
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 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
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.