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

Good morning,

I am trying to compress some data vertically and get rid of gaps (See example below). Do you have a procedure in mind that could help me?

What I have:

DatecasesStates
2002
8
Tennessee
1995
11
Wisconsin
2002
14
New York

What I need:


DatecasesStates
20028Tennessee
199511Wisconsin
200214New York

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

The right approach depends on how confident you are in the structure of your data.  If you are absolutely certain that there are always groups of 3 observations, and absolutely sure that each observation contains a single non-missing value, the answer would be simple:

data want;

set have (keep=date where=(date > .));

set have (keep=cases where=(cases > .));

set have (keep=state where=(state > ' '));

run;

You might have to adjust the WHERE clauses, depending on whether DATE and CASES are character or numeric.

But if there could be other patterns of "present" vs. "missing" data values, a more complex approach would be needed.

Good luck.

View solution in original post

4 REPLIES 4
Astounding
PROC Star

The right approach depends on how confident you are in the structure of your data.  If you are absolutely certain that there are always groups of 3 observations, and absolutely sure that each observation contains a single non-missing value, the answer would be simple:

data want;

set have (keep=date where=(date > .));

set have (keep=cases where=(cases > .));

set have (keep=state where=(state > ' '));

run;

You might have to adjust the WHERE clauses, depending on whether DATE and CASES are character or numeric.

But if there could be other patterns of "present" vs. "missing" data values, a more complex approach would be needed.

Good luck.

data_null__
Jade | Level 19

Use MISSING function to avoid the type issue.

where=(not missing(date))

stat_sas
Ammonite | Level 13

data have;

set have;

if not missing(date) then n+1;

run;

proc stdize data=have reponly out=want;

var date cases;

by n;

run;

data need(drop=n);

set want;

by n;

if last.n;

run;

data_null__
Jade | Level 19

If you're allowed to create an ID then why not just use HAVE as transaction file.

data have;
   infile cards missover;
  
input Date cases States &$16.;
  
cards;
2002    
. 8  
. . Tennessee
1995    
. 11 
. . Wisconsin
2002    
. 14 
. . New York
;;;;
   run;
data have;
   set have;
   if not missing(date) then n+1;
  
run;

data have;
   update have(obs=0) have;
   by n;
   run;
proc print;
  
run;

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!

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.

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
  • 4 replies
  • 891 views
  • 6 likes
  • 4 in conversation