BookmarkSubscribeRSS Feed
lichee
Quartz | Level 8

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;

3 REPLIES 3
PaigeMiller
Diamond | Level 26
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.

--
Paige Miller
lichee
Quartz | Level 8
Thanks Paige! The thing is that I have to check for 100+ fields, and error messages for the fields vary, rather than in a standard form. I'm required to use the separate file with error messages in case there are more fields are included to check in future.
PaigeMiller
Diamond | Level 26

Doing it this way allows you to check 100 fields and store the results in the SAS data set.

--
Paige Miller

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 3 replies
  • 393 views
  • 0 likes
  • 2 in conversation