BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pdhokriya
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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
***/

View solution in original post

11 REPLIES 11
PeterClemmensen
Tourmaline | Level 20

So you want to create theh variable D only if the variable C does not exist?

Kurt_Bremser
Super User

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;
pdhokriya
Pyrite | Level 9
Thank you for the solution, but I can except only 1 solution 🙂
Panagiotis
SAS Employee

I am a bit unclear. The C column does not exist at all, so you can't use it in a condition.

pdhokriya
Pyrite | Level 9

If C does not exits thn only i will move to b. else will consider C data only.

Shmuel
Garnet | Level 18

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;
pdhokriya
Pyrite | Level 9

This will give Note: Variable c is uninitialized. Which will not be recommended.

SuCheeTay
SAS Employee

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;

Shmuel
Garnet | Level 18

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
***/
pdhokriya
Pyrite | Level 9
Thank you so much.
pdhokriya
Pyrite | Level 9
Thank you for your help. @Shmuel AZURITE

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!

Autotuning Deep Learning Models Using SAS

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.

Discussion stats
  • 11 replies
  • 1895 views
  • 6 likes
  • 6 in conversation