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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 720 views
  • 0 likes
  • 2 in conversation