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

Suppose I have this table IN CAS:

 

PUBLIC.HAVE

ID A B
1 10 20
2 . 21
3 . .
4 11 .

 

I want to fill the blanks with the previous nonblank value for each column, by ID:

PUBLIC.WANT

ID A B
1 10 20
2 10 21
3 10 21
4 11 21

 

Normally in SAS I would use the proc expand, but since the proc expand is not CAS enabled, how would you do that in the easiest way as possible using CAS ACTIONS only (Viya 3.5)?

 

Thank you

Regards

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Ah. Seems to be the view part. 

 

Try this then. Pure data step. No views

 

data want;
   set have;
   retain aa bb;
   if a then aa = a;
   else a = aa;
   if b then bb = b;
   else b = bb;
run;

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

You can still use a data step, right?

Edoedoedo
Pyrite | Level 9
Yes I can, but it requires a little bit of coding with lags and conditions, I was wondering whether it would exists an action to quickly handle these classic use cases (I miss proc expand and proc timeseries a lot in CAS).
PeterClemmensen
Tourmaline | Level 20

I have no idea about CAS, but the data step below gives you what you want.

 

BTW yes, Proc EXPAND and TIMESERIES are awesome 🙂

PeterClemmensen
Tourmaline | Level 20

If so:

 

data have;
input ID A B;
datalines;
1 10 20
2 .  21
3 .  . 
4 11 . 
;

data temp / view = temp;
   set have;
   i = 1;
run;

data want (drop = i);
   update temp(obs=0) temp;
   by i;
   output;
run;
Edoedoedo
Pyrite | Level 9

Unfortunately that code doesn't work in CAS:

 

cas session;

libname P cas caslib="PUBLIC";

data P.have;
input ID A B;
datalines;
1 10 20
2 .  21
3 .  . 
4 11 . 
;

data P.temp / view = P.temp;
   set P.have;
   i = 1;
run;

data P.want (drop = i);
   update P.temp(obs=0) temp;
   by i;
   output;
run;
95   data P.temp / view = P.temp;
96      set P.have;
97      i = 1;
98   run;
ERROR: P.TEMP.VIEW cannot be opened because files of type VIEW are not supported in the P library.
ERROR: Unable to save DATA STEP view P.TEMP.
PeterClemmensen
Tourmaline | Level 20

Ah. Seems to be the view part. 

 

Try this then. Pure data step. No views

 

data want;
   set have;
   retain aa bb;
   if a then aa = a;
   else a = aa;
   if b then bb = b;
   else b = bb;
run;
Edoedoedo
Pyrite | Level 9

Thanks! I adapted to work in CAS, and yes it works:

 

data P.want / single=yes;
   set P.have;
   by ID;
   retain aa bb;
   if a then aa = a;
   else a = aa;
   if b then bb = b;
   else b = bb;
   drop aa bb;
run;

However if anyone knows if there are dedicated actions for these situations in Viya 3.5 or planned for Viya 4, that would be much appreciated!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 2068 views
  • 1 like
  • 2 in conversation