BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
TPayne
Fluorite | Level 6

Hello,

 

I have a data set with variables that I want to compare using an array, but currently have an if-then statement. 

 

Data set:

 

dtp1dtp1srcdtp2dtp2srcdtp3dtp3srcdtp4dtp4srcdtp5dtp5srcdtp6dtp6srcdtp7dtp7srcdtp8dtp8src
1/1/2016M              
2/22/2016M5/10/2016M7/19/2016M7/14/2017M        
 U5/10/2016M7/7/2016M5/1/2017M        
4/1/2016M4/5/2017M5/3/2017M U        
3/9/2016M M7/8/2016M7/20/2017M        
4/4/2016H7/11/2016H9/26/2016H4/24/2017H        

 

Each "src" column refers to the column before it (where dtp1src is the vaccination source for the dtp1 vaccination date). What I want to end up with is a error variable of any vaccination date (DTP1-DTP8) that does not have a source (DTP1SRC-DTP8SRC) and any source (DTP1-DTP8) that does not have a vaccination date (DTP1-DTP8). Below is the code I currently have. It works fine, but I feel like there is a more elegant way to flag these using an array/do loop. Any suggestions are greatly appreciated!

 

DATA CLEANING.DTP_ERROR;

SET CLEANING.VALID;
IF DTP1 NE . AND DTP1src=' ' OR DTP1=. AND DTP1SRC NE ' ' THEN ERROR= 'SOURCECHECK';
IF DTP2 NE . AND DTP2src=' ' OR DTP2=. AND DTP2SRC NE ' ' THEN ERROR= 'SOURCECHECK';
IF DTP3 NE . AND DTP3src=' ' OR DTP3=. AND DTP3SRC NE ' ' THEN ERROR= 'SOURCECHECK';
IF DTP4 NE . AND DTP4src=' ' OR DTP4=. AND DTP4SRC NE ' ' THEN ERROR= 'SOURCECHECK';
IF DTP5 NE . AND DTP5src=' ' OR DTP5=. AND DTP5SRC NE ' ' THEN ERROR= 'SOURCECHECK';
IF DTP6 NE . AND DTP6src=' ' OR DTP6=. AND DTP6SRC NE ' ' THEN ERROR= 'SOURCECHECK';
IF DTP7 NE . AND DTP7src=' ' OR DTP7=. AND DTP7SRC NE ' ' THEN ERROR= 'SOURCECHECK';
IF DTP8 NE . AND DTP8src=' ' OR DTP8=. AND DTP8SRC NE ' ' THEN ERROR= 'SOURCECHECK';
RUN;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data cleaning.dtp_error;
set cleaning.valid;
array dtp{8} dtp1-dtp8;
array dtpsrc{8} dtp1src dtp2src dtp3src dtp4src dtp5src dtp6src dtp7src dtp8src;
do i = 1 to 8;
  if dtp{i} ne . and dtpsrc{i} =' ' or dtp{i} = . and dtpsrc{i} ne ' ' then error = 'SOURCECHECK';
end;
run;

View solution in original post

3 REPLIES 3
Reeza
Super User
Arrays are one of the easier SAS components. This tutorial goes through arrays and how to set it up. One is very close to your situation.

1. Declare 2 arrays, one for DTP series and one for DTP1src series.
2. Change the reference from the vairable to the array with an i
3. Wrap it in a loop.

And this would be infinitely easier if you had your data set in the long format previous compared to this wide format.

You can also look into NMISS() or CMISS() to count missing if that may help.
Kurt_Bremser
Super User
data cleaning.dtp_error;
set cleaning.valid;
array dtp{8} dtp1-dtp8;
array dtpsrc{8} dtp1src dtp2src dtp3src dtp4src dtp5src dtp6src dtp7src dtp8src;
do i = 1 to 8;
  if dtp{i} ne . and dtpsrc{i} =' ' or dtp{i} = . and dtpsrc{i} ne ' ' then error = 'SOURCECHECK';
end;
run;
Kurt_Bremser
Super User

And I strongly second @Reeza 's comment about reasonable data structures. With a properly structured dataset, the code would be simple as that, and you wouldn't waste space for all the missing values.

See Maxim 33: Intelligent data makes for intelligent programs.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 352 views
  • 0 likes
  • 3 in conversation