BookmarkSubscribeRSS Feed
vraj1
Quartz | Level 8

I want to create a line which should say if i do not have a record named 'Missing variable ' i need to create it and assign values as 0 and if present leave as it is.

 

Can anyone help me in this.

 

 

 

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Without more specific information, its impossible to give you the exact code

 

However, the steps would be:

 

  1. Search for record named "missing variable"
  2. If not found, create the record with values of 0
--
Paige Miller
Steelers_In_DC
Barite | Level 11

Give an example of the data you have and what you want.  I'm having trouble imagining:  looking for 'missing variable' and adding record with value of 0.

dcruik
Lapis Lazuli | Level 10

As others have mentioned, it would be a lot easier if you provided some example data and what you are looking the output to be.  Just guessing here and thinking what I would do to search for a variable within a data set, I came up with the following.  Not sure if this is what you're looking for or not.

 

data have1;
input ID$ Value;
datalines;
1 100
2 200
;
run;

data have2;
input ID$;
datalines;
1
2
;
run;

options mprint symbolgen mlogic;
%macro missing(lib,data,var);
proc sql noprint;
select name into: Variables separated by ","
from sashelp.vcolumn
where libname="&lib" AND memname="&data";
quit;

data _NULL_;
check=find("&Variables","&var");
call symput("check",check);
run;

%if &check=0 %then %do;
	data &lib..&data;
	set &lib..&data;
	&var=0;
	run;
%end;
%mend;

%missing(WORK,HAVE1,Value)
%missing(WORK,HAVE2,Value);
FreelanceReinh
Jade | Level 19

I am not familiar with the concept of a "record name" either, but as people have already started guessing, I would like to suggest the following as a draft you could start with (under certain assumptions):

data have;
length record_name $20;
input record_name a b c;
cards;
abc    3 1 4
defghi 1 5 9
jklm   2 6 5
;

data want;
set have end=last;
array numvar[*] a--c; /* replace a--c by the list of variables you want to set to zero */
output;
missvarflag+(record_name='Missing variable'); /* replace "record_name=..." by your criterion for a record named ... */
if last & ~missvarflag then do;
  record_name='Missing variable'; /* see above */
  do _n_=1 to dim(numvar);
    numvar[_n_]=0;
  end;
  output;
end;
drop missvarflag;
run; 

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 5088 views
  • 0 likes
  • 5 in conversation