How to add system date in the title - sas

How can I add the system date in the body and by doing this, will it generate a new file without replacing the old one? Thanks in advance.
ods listing CLOSE;
ods HTML path="drive:\folder" body='title-(systemdate).html' style =
styles.test;
OPTION LS=195 PS=500 ;
TITLE; FOOTNOTE;

You can use the automatic macro variable &sysdate9.
body="title-(&sysdate9.).html"
Make sure you use double quotes.
If you don't want that format, then you can use that variable and a putn call to reformat it.
%let new_dt = %sysfunc(putn("&sysdate9."d,mmddyyd10.));
...
body="title-(&new_dt.).html"
Note: &sysdate9 is set when the session starts. It is not the date as of being called, it is as of SAS starting up.
If you want the current date, use date() function.
%let new_dt = %sysfunc(putn(%sysfunc(date()),mmddyyd10.));
...
body="title-(&new_dt.).html"

Related

I am trying to modify and make permanent changes in the SAS data set, but I am not able to do so

I want to make permanent changes is the variable name Fclass and label Date as Departure date.
i have used the modify statement along with rename but I am getting error when I am running the program.
proc datasets library= ia;
modify passngrs;
rename FClass= First Class;
label Date='Departure date';
format Date date9.;
run;
You might want to do any of these:
Rename to a variable name that does not contain a space
rename FCLASS = FirstClass;
Rename to a trickier name by using session options and a name literal
options validvarname=any;
proc ...;
...;
rename FCLASS = 'First Class'N;
Use a label for FCLASS instead of renaming it
label FCLASS = 'First Class';

How do i convert a date column using SAS Macro?

I am a SAS Developer. I currently have a script that will read from a table column that is in datetime format. In this script, it looks something like this:
data a; batch_dttm = '01Jan2011:00:00:00'dt; run;
proc sql; select batch_dttm format=16.0 into:batch_dttm from a; quit;
So when I assign it to macro variable, it is actually assigning the value of 2930485 into batch_dttm.
The problem is, when I want to resolve this &batch_dttm in another job at a later stage, i have to use this:
input(&batch_dttm,16.0)
to convert 2930485 into date.
I don't want to resolve in this way as this is the only Macro variable that has to be resolved with this input function. I want to assign 01Jan2011:00:00:00 (as text?) in PROC SQL INTO statement so that i dont have to use input conversion anymore.
I want to call &batch_dttm as datetime format in another script. I only want to resolve the datetime using "&BATCH_DTTM"dt instead of input(&batch_dttm,16.0). I believe there is a step to convert 01Jan2011:00:00:00 into text without changing it to 2930485. Is there anyway to do so?
How can I add 1 more step to make me resolve the macro in below script:
"&batch_dttm"dt
Why do you think you need to add the INPUT() function? You can just use the value of the macro variable to generate the number of seconds, 2930485 in your example, into your code.
SAS stores datetime values as a number of seconds, so these two expressions are the same:
where batch_dttm = 2930485 ;
where batch_dttm = '01JAN2011:00:00:00'dt ;
Which means you can just use code like this to use your original macro variable.
where batch_dttm = &batch_dttm ;
If you do need to have the human friendly text in the macro variable, perhaps to use in a title statement, then just change the format you use when creating the macro variable.
select batch_dttm format=datetime20. into :batch_dttm trimmed ...
...
title "Data as of &batch_dttm";
where batch_dttm = "&batch_dttm"dt ;
You can also use %sysfunc() to call PUTN() to change the existing number of seconds into that style if you want.
select batch_dttm format=32. into :batch_dttm trimmed ...
...
title "Data as of %sysfunc(putn(&batch_dttm,datetime20.))";
where batch_dttm = &batch_dttm ;
Like this?
proc sql;
select put(batch_dttm, datetime20.) into:batch_dttm from a;
quit;
%put &batch_dttm;

SAS set statement using colon and creating a filename variable

So using SAS, I have a number of SAS monthend datasets named as follows:
mydata_201501
mydata_201602
mydata_201603
mydata_201604
mydata_201605
...
mydata_201612
Each has account information at particular monthend. I want to stack the datasets all into one dataset using colon rather than writing out the full set statement as follows:
data mynewdata;
set mydata_:;
run;
However there is no datestamp variable within the datasets so when I stack them I will lose the monthend information for each account. I want to know which line refers to which monthend for each account. Is there a way I can automatically create a variable that names the table the row come from. for example the long winded way would be this:
data mynewdata;
set mydata_201501 (in=a) mydata_201502 (in=b) mydata_201503 (in=c)...;
if a then tablename = 'mydata_201501';
if b then tablename = 'mydata_201502';
if c...
run;
but is there a quicker way using colon along these lines?
data mynewdata;
set mydata_:;
tablename = _tablelabel_;
run;
thanks
I always find clicking on comment links annoying, so hopefully here's the answer in your context. Use the INDSNAME= SET statement option to assign the dataset name to a variable:
data mynewdata;
set mydata_: indsname=_tablelabel_;
tablename = _tablelabel_;
run;
N.B. you can call _tablelabel_ whatever you want, and you may wish to change it so it doesn't look like a SAS generated variable name.
INDSNAME= only became a SAS SET statement option in version 9.2
Just to be clear, with my particular code, where the datasets were named mydata_yyyymm and I wanted a monthend variable with datestamp, I was able to produce this using the solution provided by mjsqu as follows (obs and keep statement provided if required):
data mynewdata;
set mydata_: (obs=100 keep=xxx xxx) indsname=_tablelabel_;
format monthend yymmdd10.;
monthend = input(scan(_tablelabel_,-1,'_'),yymmn6.);
run;

Use a macro variable to when saving to a permanent SAS data set

Trying to save data set Keepmerge as a permanent SAS data set called Oct15Tot using the code below. If I sub "&OutTabTot" for just Oct15Tot, it works. Trying to save myself from having to chang another bit of code further down (the %let is referenced at the beginning, and is used throughout my program. Thanks!
%let OutTabTot = Oct15Tot;
libname WorkItem "\\WRKGRP\CVOWB\SAS Data Sets";
data WorkItem."&OutTabTot";
set work.Keepmerge;
run;
Here's the error I'm getting:
22
201
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, /, ;,
_DATA_, _LAST_, _NULL_.
ERROR 201-322: The option is not recognized and will be ignored.
If you remove the quotes in your Data statement it should work, like so:
%let OutTabTot = Oct15Tot;
libname WorkItem "\\WRKGRP\CVOWB\SAS Data Sets";
Data WorkItem.&OutTabTot;
Set Work.Keepmerge;
Run;
In general, as cherry notes, you should just skip the quotations.
However, if you have reason to use quotations, you need to use an n afterwards to tell SAS to make this a name literal.
%let OutTabTot = Oct15 Tot;
options validmemname=extend;
libname WorkItem "\\WRKGRP\CVOWB\SAS Data Sets";
Data WorkItem."&OutTabTot"n;
Set Work.Keepmerge;
Run;
I don't recommend using things like dataset names with spaces if you can avoid it, as it's a pain... but it's legal, with options validmemname=extend set.

SAS - code to dynamically count columns and sum each of them

I’m wondering if someone can help with a coding problem I have.
Background – I have a project that imports some files and uses the data in those files to perform projections. The contents of the files determines some aspects of the size of the output that follows. Simply, values in data loaded in drives the size and shape of the tables that follow, and this can vary.
The following code is an example of the problem.
The data loaded will have a variable year start (note wf2009, 2009 is the first year) and variable range (this example goes from 2009 to 2030, but this will vary too).
proc summary data= labeled_proj_data_hc;
class jurisdiction specialty measure;
types jurisdiction*specialty*measure;
VAR wf2009--wf2030;
output out= sum_labeled_proj_data_hc
sum(wf2009) = y2009
sum(wf2010) = y2010
sum(wf2011) = y2011
sum(wf2012) = y2012;
run;
Where I’m not sure how to proceed is:
sum(wf2009) = y2009
sum(wf2010) = y2010
sum(wf2011) = y2011
sum(wf2012) = y2012;
In the sequence of lines calling for the sum of their respective columns, how can I make this dynamic so that the start year is populated from a variable and it increments yearly until the last year which is also variable.
Has anyone solved a similar problem.
Cheers,
Is renaming the variables necessary? If not then you can use the : wildcard operator to access all variables that begin with 'wf', then just put SUM= in the output statement, which will preserve the original names.
So your proc summary would look like this.
proc summary data= labeled_proj_data_hc;
class jurisdiction specialty measure;
types jurisdiction*specialty*measure;
VAR wf: ;
output out= sum_labeled_proj_data_hc
sum=;
run;