Checking if a var exisits in a datset and if not creating it in the dataset

Accepted Solution Solved
Reply
Contributor Kc2
Contributor
Posts: 31
Accepted Solution

Checking if a var exisits in a datset and if not creating it in the dataset

[ Edited ]

My initial dataset has 5 treatments categories.

I created a macro program to run multiple time with different where conditions.

The problem is that some of the where  condition not all 6 tretament are present and this generates erros inthe subsequnet steps.

I have added this code to check wif all the treatment variables are present

 

%macro VarExist(ds,var,varnew);

%global rc dsid result ;

%let dsid=%sysfunc(open(&ds.));

%if %sysfunc(varnum(&dsid.,&var.))=0 %then %do;

%put NOTE: Var &var does not exists in &ds;

&varnew.=0;

%end;

%let rc=%sysfunc(close(&dsid));

%mend VarExist;

 

%put %VarExist(ae,_1,1);

%put %VarExist(ae,_3,3);

 

However the new var is not getting added to the dataset.What am I doing wrong?

 

thanks


Accepted Solutions
Solution
‎06-07-2016 11:52 AM
Respected Advisor
Posts: 4,828

Re: Checking if a var exisits in a datset and if not creating it in the dataset

You can add missing variables simply by naming them:

 

/* Create dataset with missing trt2 */
data test1;
trt1 = 1; trt3 = 3;
run;
proc print; run;

/* Make sure trt1, trt2 and trt3 are present */
data test1;
trt1 = 0; trt2 = 0; trt3 = 0;
set test1;
run;
proc print; run;
PG

View solution in original post


All Replies
Solution
‎06-07-2016 11:52 AM
Respected Advisor
Posts: 4,828

Re: Checking if a var exisits in a datset and if not creating it in the dataset

You can add missing variables simply by naming them:

 

/* Create dataset with missing trt2 */
data test1;
trt1 = 1; trt3 = 3;
run;
proc print; run;

/* Make sure trt1, trt2 and trt3 are present */
data test1;
trt1 = 0; trt2 = 0; trt3 = 0;
set test1;
run;
proc print; run;
PG
Super User
Posts: 7,476

Re: Checking if a var exisits in a datset and if not creating it in the dataset

Your macro does not generate code that actually works with the dataset (no data step present). Therefore nothing is done, and the result of

&varnew.=0;

is most probably only an ERROR message in the log.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,736

Re: Checking if a var exisits in a datset and if not creating it in the dataset

Why do you need a macro for this?  SAS keeps all its metadata in views and datasets in the SASHELP library - of which you will find many examples on this forum.  In your case, you can do all the checking in one datastep:

/* I want to check the dataset class which is in sashelp, to see if */
/* name sex age height weight are present */

data want (keep=libname memname passorfail);
  set sashelp.vcolumn (where=(libname="SASHELP" and memname="CLASS")) end=last;
  retain count;
  if _n_=1 then count=5;
  if upcase(name) in ("NAME","SEX","AGE","HEIGHT","WEIGHT") then count=count-1;
  if last and count>0 then do;
    passorfail="Fail";
    output;
  end;
  else if last and count=0 then do;
    passorfail="Pass";
    output;
  end;
run;

Note this is just an example, you would need to fit it in with what yuo have/want which you haven't provided, so I am just guessing.

Contributor Kc2
Contributor
Posts: 31

Re: Checking if a var exisits in a datset and if not creating it in the dataset

I do not get any error in my program:

NOTE: There were 23 observations read from the data set WORK.AE_20.

NOTE: The data set WORK.T_AE has 10 observations and 9 variables.

NOTE: PROCEDURE TRANSPOSE used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

 

NOTE: Var _1 does notexists in t_ae

1=0; 1;

2;

NOTE: Var _3 does notexists in t_ae

3=0; 3;

4;

5;

NOTE: Var _6 does notexists in t_ae

6=0; 6;

7;

 

I know how to check if the variable exists or not. What I do want to do is that if the varible does not exist it should be created and its value set to missing. Since the dataste is open I want it to created in the dataset the missing varible. The problem is that since the code is run multiple time with a different where condition, the missing variable changes. Sometimes it is treatment 1 and 3 sometimes it is treatment 4 and 5 and sometimes no treatment is missing.

 

Super User
Super User
Posts: 7,736

Re: Checking if a var exisits in a datset and if not creating it in the dataset

In which case your question is answered by @PGStats.  Create a dataset and set all the values to zero which you expect to see, then set your data on and the variables will all be there:

data test1;
  trt1 = .; trt2 = .; trt3 = .;
  set test1;
run;

 As for the multiple where, I don't understand, show test data and require output.   

Contributor Kc2
Contributor
Posts: 31

Re: Checking if a var exisits in a datset and if not creating it in the dataset

Thank you all you rock!!!!

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 393 views
  • 3 likes
  • 4 in conversation