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

Hi,

Please help me find the solution.

 

I have data like this.

id  day1 day2 day3 day4 day5 day6 day7 day8 day9 day10..

1      .       .        3       .        .       6       .         8       .        .

2      .       .        .        4      .        .       7         .        .        10

3     1      2       .        .       .        .      .          .        .         .

4     1      .        3        .       .       .        7         .        .         .

5     .       .        .         .       5      .        .          .        .         .

 

I want to pull out values not missing.. like this,

id   var1   var2    var3    var4    var5 ...

1      3       6         8          .          .

2      4       7         10        .          .

3      1       2          .        .           .

4      1       3         7        .            .

5      5       .          .         .            .

 

day variables are over 2000, observations are over 500000.

Can anyone help me?

Thanks! 😄

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, the simple answer is normalise your data:

proc transpose data=have out=want;
  by id;
  var day:;
run;

data want;
  set want;
  where _result_ ne .;
run;

/* And then if you have to transpose again */
proc transpose data=want out=t_want;
  by id;
  var _result_;
run;

You will find 99% of programming tasks are easier with normalised data.  The only time to use transposed data is in an actual repotr file - i.e. at the time of proc report.  At any other programming moment, by group processing is far simpler to code, and maintain, and uses less resources.  The alternative is a messy, harder to maintain array type coding (which of course you woul need to do again and again for each step):

data want;
  set have;
  array day{10};
  array var{10} 8;
  j=1;
  do i=1 to 10;
    if day{i} ne . then do;
      var{j}=day{i};
      j=j+1;
    end;
  end;
run;
    

Now the code doesn't look too bad for that one check, but you need to know how many var values are used, and how many day values are up front etc.  Makes code harder to maintain.

View solution in original post

1 REPLY 1
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, the simple answer is normalise your data:

proc transpose data=have out=want;
  by id;
  var day:;
run;

data want;
  set want;
  where _result_ ne .;
run;

/* And then if you have to transpose again */
proc transpose data=want out=t_want;
  by id;
  var _result_;
run;

You will find 99% of programming tasks are easier with normalised data.  The only time to use transposed data is in an actual repotr file - i.e. at the time of proc report.  At any other programming moment, by group processing is far simpler to code, and maintain, and uses less resources.  The alternative is a messy, harder to maintain array type coding (which of course you woul need to do again and again for each step):

data want;
  set have;
  array day{10};
  array var{10} 8;
  j=1;
  do i=1 to 10;
    if day{i} ne . then do;
      var{j}=day{i};
      j=j+1;
    end;
  end;
run;
    

Now the code doesn't look too bad for that one check, but you need to know how many var values are used, and how many day values are up front etc.  Makes code harder to maintain.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 706 views
  • 0 likes
  • 2 in conversation