BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
how to get the max number in a column name.
say for instance if there are columns sls1 to sls12, how to get the number 12 from the column name sls12?
6 REPLIES 6
data_null__
Jade | Level 19
> how to get the max number in a column name.
> say for instance if there are columns sls1 to sls12,
> how to get the number 12 from the column name sls12?

I think the short answer is use VNAME function. But I assume you want a method that allows you retrieve the dimension of the enurmerated list no matter what it is. To do that you need to give a little more information on what you have and what you know.
SASPhile
Quartz | Level 8
The reason is to rename the variables.i.e 12 months of sales we will have sls1 to sls12. after 12 months, the 13th month we will have new sales data and that will be called as sls1.
and the previous months sales will be named from there on. i.e sls1 has to be renamed sls2, sls2 has to be renamed as sls3 so on.
Peter_C
Rhodochrosite | Level 12
I think you would find it easier to manage the data with the month as a date column (i.e. transposed to "narrow").
Let the reporting procedures (like REPORT and TABULATE) achieve the transpose of 12 rows into 12 columns. (easy as class or accross variables). Then your filtering is simply for sales within whatever date range is needed.
I would even encourage you to transpose any preliminary data delivered in a "wide" form, back to "narrow" to better support trend analysis.
Holding a "monthly" categorical variable as a SAS date will ensure you can keep proper chronological order ( imagine arranging MonYY month strings ) with any month-type format. Then, whether your months history is 1-12 or 2-13, is no longer important.
just my $0.02
peterC
Peter_C
Rhodochrosite | Level 12
> The reason is to rename the variables.i.e 12 months
> of sales we will have sls1 to sls12. after 12 months,
> the 13th month we will have new sales data and that
> will be called as sls1.
> and the previous months sales will be named from
> there on. i.e sls1 has to be renamed sls2, sls2 has
> to be renamed as sls3 so on.

are you seeking a renaming shortcut?
from a table holding columns sls1 - sls&n which are to be renamed as sls2 - sls%eval( &n +1 )
???
(if you have SAS9.2 rename in ranges works) try[pre]
libname your (work) ;[/pre]*first create data set with columns sls1-sls12;[pre]data your.original ;
retain sls1-sls12 123 ;
run;
proc contents data= your.original noprint out= _data_ ; run; [/pre]* getting column names;[pre]proc sql noprint ;
select max( input(substr( name, 4, 3), 3.)) into :N separated by ' '[/pre]without that input function, I got the maximum of the string which was 9, but we want 12[pre]from &syslast where lowcase(substr(name,1,3)) = "sls"
;
quit ;[/pre]* finally build new table renaming columns;[pre]data new_names ;
set your.original( rename= ( sls1-sls&N = _sls1-_sls&N ) );
rename _sls1-_sls&N = sls2-sls%eval( &N +1 ) ;
run;[/pre] needed the two stage rename at the end because it seems to validate each rename within the statement.
jonam
Calcite | Level 5
please check the vcolumn view from sashelp. This is a system created view which has an entry for every column, i mean each row represents a column in a table for all the active library in that session.

Lets say your table is sales_yrly

/*Creating SALES data */
data SLS_YRLY;
array sls{12};
do j=1 to 10;
do i=1 to dim(sls);
sls{i}= floor(ranuni(0)*60);
end;
output;
end;
drop i j;
run;

proc sql;
select COUNT(name) INTO :MAX_SLS_COL from sashelp.Vcolumn where libname='WORK'
and memname='SLS_YRLY' and UPCASE(name) like 'SLS%' ;
quit;

%PUT &MAX_SLS_COL.;

Hope this helps!
Ksharp
Super User
Do these variables have the same type of data?
If they are, You can use array to get it. It suited for var1-varN.
Some dummy code like this:

[pre]
array sls{*} sls:;
max_number=dim(sls);

[/pre]

Ksharp Message was edited by: Ksharp

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