BookmarkSubscribeRSS Feed
emaguin
Quartz | Level 8

Q1: I have a dataset that has become disordered in the sense that items from a given measure/scale have become scattered across the dataset. I want to re-order the dataset so that common items are grouped together and within the group are ordered sequentially. I had asked about this previously and after some discussion among persons replying the recommendation was to use a retain statement.(Why re-order? Because i need to something with all variables associated with a scale or group of scales and i can define an array referencing all variables in the dataset and for a given scale(s) operate on those items by using element range in the Do statement.)

So (from the log):

 

17 * reorganize the dataset to bring all items in a measure together;
18 * group measures together into an AOD group, dsa1 group, and an all group;
19 data work5;
20 retain Year--Ddr2 Dalc5a--Dalc5f Dyaq1-Dyaq9 Ddmq1-Ddmq12
ERROR: Variable Year cannot be found on the list of previously defined variables.
ERROR: Variable Dalc5a cannot be found on the list of previously defined variables.
21 Dsa1-Dsa13 Drr1-Drr31 Dcs1-Dcs15 Dcs16 Dsbi1-Dsbi47
22 Doa1-Doa40 Dnsa1--Dnsa15 Dncs1-Dncs16 Dnsb1-Dnsb47
ERROR: Variable Dnsa1 cannot be found on the list of previously defined variables.
23 Dmood--Dmis4 Dass1--Dass16 Dass17 Dpb1-Dpb19 DCovid1-DCovid6 DCovid7
ERROR: Variable Dmood cannot be found on the list of previously defined variables.
ERROR: Variable Dass1 cannot be found on the list of previously defined variables.
24 Dscsev Dscs1-Dscs6 DcovSt1-DcovSt4;
25 drop i dot YB miss1-miss10 j;
26 set PNA.CleanFile;
27 run;

 

Each of the variables identified as "cannot be found" are very definitely in the set dataset PNA.CleanFile. What am i doing wrong and how do i correct it?

 

Q2: How do i find questions i have previously submitted (and replies to those questions)?

 

 

 

 

9 REPLIES 9
PaigeMiller
Diamond | Level 26

Q1: Move the SET statement to be before the RETAIN statement.

Q2: click on your user name

 

Please post logs in the window that appears when you click on </>, and not as plain text.

Insert Log Icon in SAS Communities.png

--
Paige Miller
emaguin
Quartz | Level 8
Thank you but No. Doesn't have the desired result. Variables are not reordered. Also reply that suggested I use Retain used the arrangement of commands I showed here. It seemed odd to me at the time but it worked. Why? No idea. SAS mystery.
PaigeMiller
Diamond | Level 26

To re-order variables by using a RETAIN statement before the SET statement, you cannot use the double-dash, such as Year--Ddr2.

 

Honestly, I have always thought that the idea of re-ordering data in SAS data sets is unnecessary and a waste of time.

 

On the other hand, if you are outputting results as a printed output table, then re-ordering variables makes sense, and this can be done in PROC PRINT or PROC TABULATE or PROC REPORT and probably other ways that I haven't thought of. And when you use PROC PRINT/TABULATE/REPORT then you can use the double-dash such as Year--Ddr2.

--
Paige Miller
ballardw
Super User

@emaguin wrote:
Thank you but No. Doesn't have the desired result. Variables are not reordered. Also reply that suggested I use Retain used the arrangement of commands I showed here. It seemed odd to me at the time but it worked. Why? No idea. SAS mystery.

IF it worked it would be with NUMERIC variables. The first use of a variable name sets the order in the SAS data set vector. The -- list requires an existing data source to read from. If you want to use non-sequential (in SAS ) variable names like Dalc5a you would have to explicitly list all of them. And if the variables are character they would need to have some indication of length otherwise they would be created as numeric variables.

 

You can use a LENGTH statement as well to force a variable, but has the same issue with attempting to use -- lists.

 

Personally:

"I have a dataset that has become disordered in the sense that items from a given measure/scale have become scattered across the dataset. " means go back to the code CREATING these variables and fix the order there if it is that critical.

Or write out all of the variable names and make sure to indicate type/length and not use the -- list.

 

Personally I might also consider why you have names like Dalc5a--Dalc5f instead of Dalc5_1--Dalc5_6. You could have used the Dalc5_1-Dalc5_6 single dash list with a Length statement setting type and length to character of not numeric.

FreelanceReinh
Jade | Level 19

Hello @emaguin,

 

The name range lists (using the double hyphen) require that the variables in the list already exist in the program data vector (PDV). However, the order of the variables is determined by the PDV. So, it's a dilemma: The RETAIN statement before the SET statement (which populates the PDV) comes too early for using the name range lists, but after the SET statement it comes too late to reorder the variables.

 

You say that the dataset "has become disordered" -- indicating that you might have an "old" dataset with the desired variable order. In this case you could use that dataset in the SET statement (with the OBS=0 dataset option) to determine the variable order as you want:

data work5;
set old_ds(obs=0) PNA.CleanFile;
run;

If you don't have a suitable "old_ds", you can build the PDV step by step. For example:

data work5;
set PNA.CleanFile(obs=0 keep=Year--Ddr2)
    PNA.CleanFile(obs=0 keep=Dalc5a--Dalc5f)
    PNA.CleanFile(obs=0 keep=Dyaq1-Dyaq9)
    PNA.CleanFile(obs=0 keep=Ddmq1-Ddmq12)
    ...
    PNA.CleanFile;
drop i dot YB miss1-miss10 j;
run;

 

Tom
Super User Tom
Super User

Variables that are sourced from an input dataset (such as your PNA.CleanFile dataset) are automatically retained.

Why do you have them in your RETAIN statement?

What are you actually trying to do?

 

If you are trying to change the order of the variables perhaps you should instead use the KEEP= dataset option?

data work5;
   set PNA.CleanFile(keep=Year--Ddr2);
   set PNA.CleanFile(keep=Dalc5a--Dalc5f);
   retain Dyaq1-Dyaq9 Ddmq1-Ddmq12
          Dsa1-Dsa13 Drr1-Drr31 Dcs1-Dcs15 Dcs16 Dsbi1-Dsbi47
          Doa1-Doa40
   ;
   set PNA.CleanFile(keep= Dnsa1--Dnsa15);
   retain Dncs1-Dncs16 Dnsb1-Dnsb47 ;
   set PNA.CleanFile(keep=Dmood--Dmis4);
   set PNA.CleanFile(keep=Dass1--Dass16);
   retain Dass17 Dpb1-Dpb19 DCovid1-DCovid6 DCovid7
          Dscsev Dscs1-Dscs6 DcovSt1-DcovSt4
   ;
   set PNA.CleanFile;
   drop i dot YB miss1-miss10 j;
run;
emaguin
Quartz | Level 8
Tom (I'm asking you because we've corresponded before and recognize your name), does this communication system function like a listserv. That is everybody subscribing/is a member sees everybody's posts? Or does it function like a standard telephone, you and I talk but nobody else know what we talked about? Or does it function like a conference, everybody that joins my call hears my question and what everybody else on call said to me.
I'm asking this because, counting yours, I got five replies and not all the same and some raised secondary points I hadn't/don't understand the implications of and I'd like to follow-up.
Thanks, Gene Maguin


Tom
Super User Tom
Super User

The questions are like a listserv.  Everyone can see the questions and answers.

There is a direct message facility to send one to one messages.

PaigeMiller
Diamond | Level 26

I submit that one-to-one messages are the wrong tool for providing answers to SAS problems. The discussion about SAS problems (like this thread) should be public so that everyone can learn and everyone can benefit; and so multiple solutions can be provided.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 9 replies
  • 766 views
  • 1 like
  • 5 in conversation