Help using Base SAS procedures

Retain statement

Reply
N/A
Posts: 0

Retain statement

I'm just starting using SAS...I received a file in his format:

Client...SKU....Volume
3020....1000.....10
...........1001......3
............1002..... 7
5505.... 2001.....15
..... ......1005 .....6
............1004.....30

I need to complete the blank with the previus value, for exemle, for line2 the client is 3020 too. How could i do it? A friend toldmeto use Retain statement but did not work.

I tried this code:

Data test;
Set test;

Retain Client;
If _n_=1 then Client=NEW_CLIENT;
Else do;
If NEW_CLIENT=’’ then NEW CLIENT=CLIENT;
Else Client=NEW_CLIENT;
End;
Run;

Thanks
Super Contributor
Super Contributor
Posts: 3,174

Re: Retain statement

The _N_ condition is only in effect with the first observation. You will need to use a sorted input file and use a BY statement with the key variables, and retain the important variable(s) using the IF FIRST. THEN ; condition.

Check the SAS support http://support.sas.com/ website for DATA STEP PROGRAMMING discussion and using a BY statement and the FIRST. and LAST. conditions for additional details.

Scott Barry
SBBWorks, Inc.
Valued Guide
Posts: 2,174

Re: Retain statement

data latest( keep= client sku volume ) ;
* assuming the name of the raw data is CLIENTS ;
set clients( obs= 0 ) ; * getting column definitions ;
do while( not finished ) ;
set clients( rename= ( client= rawC )) end= finished ;
client = coalesce( rawC, client ) ;
output ;
end ;
stop ;
run ;

if column CLIENT is character, use function COALESCEC() rather than COALESCE()


Message was edited by: Peter.C getting column definitions was intended as comment, prefix with *


Message was edited by: Peter.C
N/A
Posts: 0

Re: Retain statement

getting column definitions - Sas did not recognized. I couldn't make it work

I'm not a programmer...I'm a mkt consutant and i have a huge amont of data to manipulate. Message was edited by: elilika
Super Contributor
Super Contributor
Posts: 3,174

Re: Retain statement

If willing to solve the problem (regardless of your focus/perspective), you will need to explain what code you attempted and exactly what error occurred or further explain "getting column definitions - Sas did not recognized. I couldn't make it work".

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: Retain statement

Try the following Code. Hope this will help.



data test;
input client sku volume ;
cards;
3020 1000 10
. 1001 3
. 1002 7
5505 2001 15
. 1005 6
. 1004 30
;
run;

data test1(drop=client rename=(temp=client));
set test;
by client notsorted;
retain temp;
if first.client and client ne . then temp=client;
client=temp;
run;

~Sukanya E
N/A
Posts: 0

Re: Retain statement

Thanks Sukanya, it worked!
:-) Message was edited by: elilika
Valued Guide
Posts: 2,174

Re: Retain statement

sorry my proposal had a mistake.
As a comment, the text "getting column definitions" should have been prefixed with an asterisk.
I have updated the code in the earlier message

hope it works for you now.
good luck

PeterC
Ask a Question
Discussion stats
  • 7 replies
  • 167 views
  • 0 likes
  • 3 in conversation