BookmarkSubscribeRSS Feed
mnew
Calcite | Level 5
Hi Experts:

What to do when you have to define a new variable's length in a data step but don't want it to be the first variable in the new dataset?

In the example below, I don't want State to be the first variable. But I don't really know how to move it to be after address2 or move it to be the last variable. (I guess I could define every variable in the Length statement but what if there are many variables in the dataset?)

Thank you!

Data work.talent2;
Length State $ 3;
Set sasuser.talent(obs=5);
State=scan(address2,2,',');
Run;
Proc print data=work.talent2;
Run;
10 REPLIES 10
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Mnew,

To order variables in a dataset you can use a RETAIN statement listing variables in the order that you need. This statement should be placed before SET stetment, like this:
[pre]
data a;
retain A,B,C,D;
set a;
run;
[/pre]
Sincerely,
SPR
chang_y_chung_hotmail_com
Obsidian | Level 7
...
> To order variables in a dataset you can use a RETAIN
> statement listing variables in the order that you
> need. This statement should be placed before SET
> stetment, like this:
> [pre]
> data a;
> retain A,B,C,D;
> set a;
> run;
> [/pre]
...
@SPR: No. Retain does not take a comma delimited list of variables. Hardly any statements do. Use any of the variable lists.

For the re-ordering purpose, use retain before set and don't use length statements at all. Hope this helps.
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Chang_y_chung@hotmail.com,

It was typo from my side. Of course, comma is forbidden. The rest is true. RETAIN can be used to order variables in the dataset.

Sincerely,
SPR
mnew
Calcite | Level 5
Thanks for the follow up. I did test Retain (w/o comma) and it worked for me. As for Length statement in the original question, I did have to define length for the variable (not for the re-ordering).
SPR
Quartz | Level 8 SPR
Quartz | Level 8
I forgot to add that if you need LENGTH then place if after RETAIN before SET.

SPR
mnew
Calcite | Level 5
This is a good idea esp. when I want the new variable to be in the middle, not at the end of existing one. I'm glad that I don't have to type up all the variables in the Retain statement, if I just want this new one to be No.4.
Thank you!
ballardw
Super User
Put the LENGTH statement AFTER the SET but before where the variable is used. The new variables will be at the "end" of the data.
mnew
Calcite | Level 5
Tested. Good idea. Thank you!
ArtC
Rhodochrosite | Level 12
Variable order on the PDV is determined by the order that the variables are encountered during the compilation of the DATA step. As shown in the previous responses there are several ways to specify the variable in such a way as to determine the order. The SET (and MERGE etc.) statements grab the order from the incoming data set (for the variables not already seen). The FORMAT and ATTRIB statements can also be used.
mnew
Calcite | Level 5
Thank you for explaining the why and how! Now I know why our trainer spent so much time explaining the compilation phase.

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
  • 10 replies
  • 1204 views
  • 0 likes
  • 5 in conversation