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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@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

11 REPLIES 11
Quentin
Super User

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.

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Wolverine
Quartz | Level 8

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Wolverine
Quartz | Level 8
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.
Wolverine
Quartz | Level 8

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;

PaigeMiller
Diamond | Level 26

This works for me, with the RETAIN first

 

DATA retain_example; 
    RETAIN Name Age Sex;
    SET sashelp.class;
RUN;
--
Paige Miller
Wolverine
Quartz | Level 8

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
PaigeMiller
Diamond | Level 26

@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
Wolverine
Quartz | Level 8

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;

PaigeMiller
Diamond | Level 26

@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
Wolverine
Quartz | Level 8

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 11 replies
  • 6592 views
  • 2 likes
  • 4 in conversation