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

I have a dataset that has variables named Amin to Amax. E.g. A121, A122 up to A314. min and max are created in some procedure before. I would like automatically to do the following

1) to keep variables greater than e.g. A150 i.e. A150, A151, up to A(max(xx)) i.e.

 

Data newdata;

set olddata;

     keep A150-Amax;

run;

 

2) drop all above e.g. A180 i.e. keep Amin - A180 i.e.

 

Data newdata;

set olddata;

     keep Amin-A180;

run;

 

Thanks

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
kiranv_
Rhodochrosite | Level 12

something like below using dictionary.columns or much simpler and cleaner by @ballardw 

 

data have;
array A (150) (1000:1149);
run;

/* drop variables macrovariable */
proc sql;
select name into :var seperated by " " from dictionary.columns
where memname ="HAVE"
and libname ="WORK"
and input(compress(name,,"kd"),best32.) ge 45 and input(compress(name,,"kd"),best32.) le 145;

/* drop variables */
data want;
set have (drop= &var);
run;

 

View solution in original post

2 REPLIES 2
ballardw
Super User

@Zach84 wrote:

I have a dataset that has variables named Amin to Amax. E.g. A121, A122 up to A314. min and max are created in some procedure before. I would like automatically to do the following

1) to keep variables greater than e.g. A150 i.e. A150, A151, up to A(max(xx)) i.e.

 

Data newdata;

set olddata;

     keep A150-Amax;

run;

 

2) drop all above e.g. A180 i.e. keep Amin - A180 i.e.

 

Data newdata;

set olddata;

     keep Amin-A180;

run;

 

Thanks

 

 

 

 


 

For variables that have actual numbers as part of the name then: Keep A120 - A180 is fine.

But character values seldom sort properly to include in a list. So if you have an actual name like Amax then including that in a list is likely not going to be greatly helpful. If you don't know what the lowest or largest index is and need to use something like A20 we'll need to add a step to identify that and is likely going to be some additional code involved that may require sharing more about your actual data.

 

If the variables are sequential in the data then you can use a keep or drop with two dashes when the names are not as nice but are in column order:

 

A list like: Keep thisvar -- thatvar will keep all of the variables in order.

kiranv_
Rhodochrosite | Level 12

something like below using dictionary.columns or much simpler and cleaner by @ballardw 

 

data have;
array A (150) (1000:1149);
run;

/* drop variables macrovariable */
proc sql;
select name into :var seperated by " " from dictionary.columns
where memname ="HAVE"
and libname ="WORK"
and input(compress(name,,"kd"),best32.) ge 45 and input(compress(name,,"kd"),best32.) le 145;

/* drop variables */
data want;
set have (drop= &var);
run;

 

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 534 views
  • 0 likes
  • 3 in conversation