BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

  There is a dataset that has date columns from Jul1,2013 to Dec1,2011.

  Every week I need to process data taking last 36 weeks starting from the current date.

   How to use this in a  loop to use in a put statement to output it to a text file alsong with Id .

i.e put @01 id  dt_01JUL2013 dt_24JUN2013 dt_17Jun2013?

6 REPLIES 6
jakarman
Barite | Level 11

When you are asking:

I have a dataset with varying colum-names (dt_&&date&i) wanting in put statement than:

Define an array in the datastep to process all columns.

SAS(R) 9.4 Statements: Reference

The variability of all the dates can be solved by first defining macro variables with the names. 

---->-- ja karman --<-----
PGStats
Opal | Level 21

What are your columns called, exactly?

PG
SASPhile
Quartz | Level 8

Date columns are: dt_01Jun2013    dt_24Jun2013

Reeza
Super User

That's a bad way to store information, but probably besides the point now.

I think you need to provide more info on your data structure. For example, if it is weekly data the 1st of June is a Saturday, but the 24 is a monday, so how are the columns names determined. I'm assuming you'd need to dynamically generate the columns you'd need to pull. Do you have to do a data step with put or can you use a proc export or tagset to export?

PGStats
Opal | Level 21

I agree with Reeza about your data structure, but anyway, here is how to get what you want, or something close to it :


proc sql;
select name into :keepCols separated by ","
from dictionary.columns
where libname="WORK" and memname="MYTEST" and
input(substr(name,4),? anydtdte9.) >= intnx("WEEK", today(), -36, "S");
create table keep36weeks as
select id, &keepCols from mytest;
quit;

  

PG

PG
Scott_Mitchell
Quartz | Level 8

This assumes that your variables are in a sequential order.  I used week.3 because you mentioned the specific dates 01JUL2013 24JUN2013 17JUN2013.


%LET FIRSTMTH = DT_%SYSFUNC(PUTN(%SYSFUNC(INTNX(WEEK.3,%SYSFUNC(TODAY()),-36,END)),DATE9.));

%LET LASTMTH  = DT_%SYSFUNC(PUTN(%SYSFUNC(INTNX(WEEK.3,%SYSFUNC(TODAY()),-1,END)),DATE9.));

DATA WANT;

  SET HAVE (KEEP = ID &FIRSTMTH. -- &LASTMTH.);

RUN;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2185 views
  • 0 likes
  • 5 in conversation