Hi Reader,
Could you please help me with below mentioned data where Variable C does not exist in dataset.
Need to create D variable where a = "One" and Variable C is null/not exist then consider b variable data.
Output would be like this : -
a | b | d |
One | New1 | New1 |
Two | New2 | New2 |
Three | New3 | New3 |
Thank you in advance.
Regards,
Priyanka
Alternative way to check weather variable C exists:
Data test;
dsid = open('have'); /*open file to check */
if dsid then do;
_varnum = varnum(dsid,'C'); /* check does C exist */
putlog _varnum=;
end;
dsid = close(dsid);
run;
/***
_varnum = 0 for non existing C variable in HAVE dataset
otherwise _varnum > 0
***/
So you want to create theh variable D only if the variable C does not exist?
There is no variable c in your dataset, so it can't be used in a condition. It "can't even be null".
If you need to make code conditional upon the mere presence of a variable in a dataset, query DICTIONARY.COLUMNS and run code conditionally (%IF-%THEN-%DO-%END).
%let name=X;
proc sql noprint;
select name into :name
from dictionary.columns
where libname ="WORK" and memname = "HAVE" and upcase(name) = "C";
quit;
data want;
set have;
%if &name. = X
%then do;
if a = "One" then d = b;
%end;
run;
I am a bit unclear. The C column does not exist at all, so you can't use it in a condition.
If C does not exits thn only i will move to b. else will consider C data only.
Next code will do what you want:
data want;
set have;
retain c; /* if c does not exist it will be added with null value */
if a='One' then do;
if missing(c) then d=b;
else d=c;
end;
run;
This will give Note: Variable c is uninitialized. Which will not be recommended.
Taking @Kurt_Bremser code and updated to this should give you what you need. Variable d=b when c is either missing from the table or it's value is missing.
%let name=X;
proc sql noprint;
select name into :name
from dictionary.columns
where libname ="WORK" and memname = "HAVE" and upcase(name) = "C";
quit;
data want;
set have;
%if &name = X
%then %do;
d = b;
%end;
%else %do;
if c=" " then d=b;
%end;
run;
Alternative way to check weather variable C exists:
Data test;
dsid = open('have'); /*open file to check */
if dsid then do;
_varnum = varnum(dsid,'C'); /* check does C exist */
putlog _varnum=;
end;
dsid = close(dsid);
run;
/***
_varnum = 0 for non existing C variable in HAVE dataset
otherwise _varnum > 0
***/
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Follow along as SAS’ Robert Blanchard explains three aspects of autotuning in a deep learning context: globalized search, localized search and an in parallel method using SAS.
Find more tutorials on the SAS Users YouTube channel.