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

I have a wide dataset where a series of columns are named _201507, _201508, ..., _201603, to indicate the value of a variable during a specific month. I want to create an array of _201507-_201603.

 

I tried the following code:

 

array period(*) _201507-_201603;
SumPeriod = sum(of _201507-_201603);

 

 

The problem with this code is that SAS doesn't realize that column _201512 is followed by column _201601. Instead it creates columns _201513 through to _201600 which is clearly not what I want.

How can I get around this issue?

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
If they are set next to each other. Try :
array period(*) _201507--_201603;

View solution in original post

4 REPLIES 4
Ksharp
Super User
If they are set next to each other. Try :
array period(*) _201507--_201603;

udden2903
Obsidian | Level 7

Thank you for your help. This solved my problem.

 

In the original dataset, the columns were ordered randomly. I used the Query Builder in SAS EG to order them chronologically next to each other. Is there any faster way of ordering columns in situations like these?

Ksharp
Super User
I would do this. But it is not efficient .

data have;
retain _id 2;
array x{*} _201602 _201511 _201512 _201601 (4*1);
run;
proc sql;
 select name into : list separated by ' '
  from dictionary.columns
   where libname='WORK' and memname='HAVE' 
    and prxmatch('/^_\d{6}$/',strip(name))
     order by input(substr(name,2),best32.);
quit;


%put NOTE: &list  ;
SASKiwi
PROC Star

SAS variable ranges are simply numbers. There is no such thing as a SAS variable "date range". I'd also say it is probably not good practice to hard-code dates into variable names as you lose a lot of flexibility. For example if your table just contained the last 12 month's data, each month your variables would change name.

 

Why not consider naming your columns like month1 to month12 (or how ever many you want) then add a start_month and an end_month variable to specify the start and the end of the range?

 

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 1343 views
  • 1 like
  • 3 in conversation