DATA Step, Macro, Functions and more

Why doesn't this RETAIN statement work??

Accepted Solution Solved
Reply
Contributor
Posts: 57
Accepted Solution

Why doesn't this RETAIN statement work??

Did SAS change the functionality of the RETAIN statement?  I'm using SAS 9.4 M4.  I have a data set with about 130 variables in it, and I want to focus on just the "UM" variables for this particular output.  I also want to put these variables in a certain order for easier review.  The code below produces a file with all 130 variables in their original order, and there aren't any errors or a warnings in the log:

 

DATA temp.SCA_Atb_grpsdd_flag_totper; SET temp.SCA_Atb_grpsdd_flag_tot;
RETAIN

encrypted_id
UM_OP_claim_ct
UM_ED_claim_ct
UM_IP_claim_ct
UM_claim_tot
UM_percent;

 

RUN;


Accepted Solutions
Solution
a week ago
Respected Advisor
Posts: 2,827

Re: Why doesn't this RETAIN statement work??

Posted in reply to Wolverine

@Wolverine wrote:

That SHOULD work, but it does not work for me.  Here is the exact code I used:

 

DATA retain_example;
    RETAIN Name Age Sex;
    SET sashelp.class;
RUN;

proc print data=retain_example;
RUN;

 

Here is the log:

 

1    DATA retain_example;
2        RETAIN Name Age Sex;
3        SET sashelp.class;
4    RUN;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.RETAIN_EXAMPLE has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.04 seconds
      cpu time            0.01 seconds

5
6    proc print data=retain_example;
NOTE: Writing HTML Body file: sashtml.htm
7    RUN;

NOTE: There were 19 observations read from the data set WORK.RETAIN_EXAMPLE.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.70 seconds
      cpu time            0.48 seconds

 

 

 

And here is the output:

 

SAS Output

The SAS System

Obs Name Age Sex Height Weight
1 Alfred 14 M 69.0 112.5
2 Alice 13 F 56.5 84.0
3 Barbara 13 F 65.3 98.0
4 Carol 14 F 62.8 102.5
5 Henry 14 M 63.5 102.5
6 James 12 M 57.3 83.0
7 Jane 12 F 59.8 84.5
8 Janet 15 F 62.5 112.5
9 Jeffrey 13 M 62.5 84.0
10 John 12 M 59.0 99.5
11 Joyce 11 F 51.3 50.5
12 Judy 14 F 64.3 90.0
13 Louise 12 F 56.3 77.0
14 Mary 15 F 66.5 112.0
15 Philip 16 M 72.0 150.0
16 Robert 12 M 64.8 128.0
17 Ronald 15 M 67.0 133.0
18 Thomas 11 M 57.5 85.0
19 William 15 M 66.5 112.0

So the RETAIN statement has variables in the order NAME AGE SEX

 

and the PROC PRINT shows variables in the order NAME AGE SEX

 

thus, I see no problem here with the code or the results

--
Paige Miller

View solution in original post


All Replies
PROC Star
Posts: 1,450

Re: Why doesn't this RETAIN statement work??

Posted in reply to Wolverine

In order to use RETAIN to reorder variables you need to but the RETAIN statement before the SET statement.  

 

Otherwise the order will be determined by the order variables are seen on the SET statement.

Contributor
Posts: 57

Re: Why doesn't this RETAIN statement work??

This does not work either, and again does not produce any errors or warnings in the log:

 

DATA temp.SCA_Atb_grpsdd_flag_totper; RETAIN

encrypted_id
UM_OP_claim_ct
UM_ED_claim_ct
UM_IP_claim_ct
UM_claim_tot
UM_percent;

SET temp.SCA_Atb_grpsdd_flag_tot;
RUN;

Super User
Super User
Posts: 9,427

Re: Why doesn't this RETAIN statement work??

Posted in reply to Wolverine

You have not provided any information to help answer this question.  Post test data in the form of a datastep, then show what the output from that test data should be.  Show relevant code of what you have tried, logs of what errors etc.  

All I can say from that is no, they have not changed the retain functionality.

Contributor
Posts: 57

Re: Why doesn't this RETAIN statement work??

The dataset is large and contains confidential information. I could make a subset, but it would take time. And frankly, this code is as basic as it gets. I did show the relevant code of what I've tried, and there are no errors in the log.
Contributor
Posts: 57

Re: Why doesn't this RETAIN statement work??

Posted in reply to Wolverine

Here is the same code applied to an example dataset.  Again, it doesn't work, and it doesn't produce any errors or warnings in the log.

 

DATA retain_example; SET sashelp.class;
RETAIN Name Age Sex;
RUN;

proc print data=retain_example;
RUN;

Respected Advisor
Posts: 2,827

Re: Why doesn't this RETAIN statement work??

Posted in reply to Wolverine

This works for me, with the RETAIN first

 

DATA retain_example; 
    RETAIN Name Age Sex;
    SET sashelp.class;
RUN;
--
Paige Miller
Contributor
Posts: 57

Re: Why doesn't this RETAIN statement work??

Posted in reply to PaigeMiller

That SHOULD work, but it does not work for me.  Here is the exact code I used:

 

DATA retain_example;
    RETAIN Name Age Sex;
    SET sashelp.class;
RUN;

proc print data=retain_example;
RUN;

 

Here is the log:

 

1    DATA retain_example;
2        RETAIN Name Age Sex;
3        SET sashelp.class;
4    RUN;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.RETAIN_EXAMPLE has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.04 seconds
      cpu time            0.01 seconds

5
6    proc print data=retain_example;
NOTE: Writing HTML Body file: sashtml.htm
7    RUN;

NOTE: There were 19 observations read from the data set WORK.RETAIN_EXAMPLE.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.70 seconds
      cpu time            0.48 seconds

 

 

 

And here is the output:

 

SAS Output

The SAS System

Obs Name Age Sex Height Weight
1 Alfred 14 M 69.0 112.5
2 Alice 13 F 56.5 84.0
3 Barbara 13 F 65.3 98.0
4 Carol 14 F 62.8 102.5
5 Henry 14 M 63.5 102.5
6 James 12 M 57.3 83.0
7 Jane 12 F 59.8 84.5
8 Janet 15 F 62.5 112.5
9 Jeffrey 13 M 62.5 84.0
10 John 12 M 59.0 99.5
11 Joyce 11 F 51.3 50.5
12 Judy 14 F 64.3 90.0
13 Louise 12 F 56.3 77.0
14 Mary 15 F 66.5 112.0
15 Philip 16 M 72.0 150.0
16 Robert 12 M 64.8 128.0
17 Ronald 15 M 67.0 133.0
18 Thomas 11 M 57.5 85.0
19 William 15 M 66.5 112.0
Solution
a week ago
Respected Advisor
Posts: 2,827

Re: Why doesn't this RETAIN statement work??

Posted in reply to Wolverine

@Wolverine wrote:

That SHOULD work, but it does not work for me.  Here is the exact code I used:

 

DATA retain_example;
    RETAIN Name Age Sex;
    SET sashelp.class;
RUN;

proc print data=retain_example;
RUN;

 

Here is the log:

 

1    DATA retain_example;
2        RETAIN Name Age Sex;
3        SET sashelp.class;
4    RUN;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.RETAIN_EXAMPLE has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.04 seconds
      cpu time            0.01 seconds

5
6    proc print data=retain_example;
NOTE: Writing HTML Body file: sashtml.htm
7    RUN;

NOTE: There were 19 observations read from the data set WORK.RETAIN_EXAMPLE.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.70 seconds
      cpu time            0.48 seconds

 

 

 

And here is the output:

 

SAS Output

The SAS System

Obs Name Age Sex Height Weight
1 Alfred 14 M 69.0 112.5
2 Alice 13 F 56.5 84.0
3 Barbara 13 F 65.3 98.0
4 Carol 14 F 62.8 102.5
5 Henry 14 M 63.5 102.5
6 James 12 M 57.3 83.0
7 Jane 12 F 59.8 84.5
8 Janet 15 F 62.5 112.5
9 Jeffrey 13 M 62.5 84.0
10 John 12 M 59.0 99.5
11 Joyce 11 F 51.3 50.5
12 Judy 14 F 64.3 90.0
13 Louise 12 F 56.3 77.0
14 Mary 15 F 66.5 112.0
15 Philip 16 M 72.0 150.0
16 Robert 12 M 64.8 128.0
17 Ronald 15 M 67.0 133.0
18 Thomas 11 M 57.5 85.0
19 William 15 M 66.5 112.0

So the RETAIN statement has variables in the order NAME AGE SEX

 

and the PROC PRINT shows variables in the order NAME AGE SEX

 

thus, I see no problem here with the code or the results

--
Paige Miller
Contributor
Posts: 57

Re: Why doesn't this RETAIN statement work??

Posted in reply to PaigeMiller

So basically, the KEEP statement keeps only the specified variables.  The RETAIN statement puts the specified variables in the specified order, but retains ALL variables in the dataset rather than only retaining the specified variables.  That's the source of my confusion.

 

This code gives me the output I want:

 

DATA retain_example;
    RETAIN Name Age Sex;
    SET sashelp.class;
    KEEP Name Age Sex;
RUN;

proc print data=retain_example;
RUN;

Respected Advisor
Posts: 2,827

Re: Why doesn't this RETAIN statement work??

Posted in reply to Wolverine

@Wolverine wrote:

So basically, the KEEP statement keeps only the specified variables.  The RETAIN statement puts the specified variables in the specified order, but retains ALL variables in the dataset rather than only retaining the specified variables.  That's the source of my confusion.

 

This code gives me the output I want:

 

DATA retain_example;
    RETAIN Name Age Sex;
    SET sashelp.class;
    KEEP Name Age Sex;
RUN;

proc print data=retain_example;
RUN;


Even so, you are doing extra work (RETAIN statement plus KEEP statement, that's two lines of SAS code) to achieve what the VAR statement (that's one line of SAS code) in PROC PRINT will do.

--
Paige Miller
Contributor
Posts: 57

Re: Why doesn't this RETAIN statement work??

Posted in reply to PaigeMiller

Good to know, but in this case, the output file will be converted to Excel.  So I need the specified variables in the specified order in the dataset, not just what's displayed by PROC PRINT.

 

But actually, I think I have an even better solution.  PROC SQL will do both KEEP and RETAIN with only a single statement:

 

PROC SQL;
    Create table retain_SQL
    as Select Name, Age, Sex
    from sashelp.class;
QUIT;

proc print data=retain_SQL;
RUN;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 132 views
  • 0 likes
  • 4 in conversation