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! 😄
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.
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.
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.
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.