Hi All,
I wanted to check if fields are populated based on the criteria. If a field is not populated, then add error_message to indicate the related criterium. For example, in the data I have below, first_name is a field required according to the file "have_ck". When first_name is not populated, the related error_message will need to be added to the original file, as the dummy data "want" shows. Any insight is greatly appreciated!
data have;
infile datalines truncover dsd;
input dummyid first_name $ middle_name $ last_name $ DOB :mmddyy10.;
format DOB mmddyy10.;
datalines;
1 ,Joe , ,Messi ,1/2/1960
2 , ,S ,Bieber ,4/3/1958
3 ,Jenny , , ,5/5/1955
;
run;
data have_ck;
infile datalines truncover dsd;
length field $11. error_message $25.;
input field $ required error_message $;
datalines;
dummyid,1,[dummyid] is required.
first_name,1,[first_name] is required.
middle_name,0,
last_name,1,[last_name] is required.
DOB,1,[DOB] is required.
;
run;
data want;
infile datalines truncover dsd;
length error_message $25.;
input dummyid first_name $ middle_name $ last_name $ DOB :mmddyy10. error_message $;
format DOB mmddyy10.;
datalines;
1 ,Joe , ,Messi ,1/2/1960 ,
2 , ,S ,Bieber ,4/3/1958 ,[first_name] is required.
3 ,Jenny , , ,5/5/1955 ,[last_name] is required.
;
run;
data want;
set have;
if missing(First_name) then newfield1='[first_name] is required.';
if missing(last_name) then newfield2='[last_name] is required.';
run;
This flags the missing fields. In general, I would leave the SAS data set like this, rather than creating a new long string where these missing flags are appended to the existing text; your ability to work with the data set does not require appending the flags to the existing text, and as a matter of fact creating a longer text string just slows you down in the long run.
Doing it this way allows you to check 100 fields and store the results in the SAS data set.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.