BookmarkSubscribeRSS Feed
Walternate
Obsidian | Level 7

Hi,

I have a dataset with two sets of month variables. Each set has months going from 10 months before a target date to 10 months after.

ID     Target_date      mth_10_pre_a     mth_9_pre_a     mth_8_pre_a...mth_8_post_a      mth_9_post_a     mth_10_post_a mth_10_pre_b...etc.

1          2/2012                    3                         7                       .                    0                             3                    .                         .

What I want is to use values from the first set of month variables (a) to change some of the values in the second set of month variables from missing to 0; eg, since mth_10_pre_a has a value (or a 0), then mth_10_pre_b should be changed from missing to 0 (if mth_10_pre_b already had a positive value, however, it should remain unchanged).

I know that an array is theoretically the easiest way to do this, but since the numbers come in the middle of my variables, I'm not sure what's the most efficient way to go about it (ie, if there's a way to avoid changing all my variable names by hand).

Any help is much appreciated.

1 REPLY 1
ballardw
Super User

You don't give us the whole list of variables but I think what you want is an array solution. No need to change any variable names just list in order. Note that with the first list in place you can copy-paste- search and replace _a with _b so not terrible. So you likely have 21 variables (and I would guess at some time in the future more)

Data want;

     set have;

     array prea mth_10_pre_a     mth_9_pre_a  mth_8_pre_a  ; /* continue to list all of the first group of variables just paste the names from any handy source*/

     array preb mth_10_pre_b     mth_9_pre_b  mth_8_pre_b ; /* again continue matching the order of the previous line*/

     do j= 1 to dim(prea);

          if prea ge 0 and missing (preb ) then preb = 0;

     end;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1 reply
  • 692 views
  • 0 likes
  • 2 in conversation