DATA Step, Macro, Functions and more

Need help with a simple macro

Accepted Solution Solved
Reply
Contributor
Posts: 31
Accepted Solution

Need help with a simple macro

Hi

I am trying to change date in all the data sets and saving it on another location

This is what i have written so far

%macro datea(data_set,oldst );

data &data_set;

set &oldst;

new_date=input(old_DATE,date9.);

format new_date date9.;

run;

%mend datea;

%datea(&var1, &var2 );

** I need to change in such a way that I just give library name here it takes all the data set and copies to the second location with the same name

%let var1 = %str(abc.dddasas);

%let var2 = %str(cdf.dddasas);


Accepted Solutions
Solution
‎06-26-2015 05:39 AM
Super User
Super User
Posts: 7,993

Re: Need help with a simple macro

Posted in reply to MadhuKorni

A simpler method would be to use the already existing metadata which SAS provides in the form of the SASHELP.VTABLE (and VCOLUMN):

%macro mCopy (InLib=,OutLib=);

  data _null_;

    set sashelp.vtable (where=(libname=upcase("&InLib.") and memtype="DATA"));

    call execute('data &outlib..'||strip(memname)||'; set &inlib..'||strip(memname)||'; new_date=today(); format new_date date9.; run;');

  run;

%mend mCopy;

%mCopy (InLib=sashelp,OutLib=Work);

View solution in original post


All Replies
Regular Contributor
Posts: 227

Re: Need help with a simple macro

Posted in reply to rahul88888

dates are integers.

formats are look-up tables for those integers.

you do not need to create a new variable in order to change the format

i.e. the way the date is displayed.

Here is the reference book to have

The Essential Guide to SAS Dates and Times, Second Edition

Contributor
Posts: 31

Re: Need help with a simple macro

Posted in reply to Ron_Fehd_macro_maven

Thank you sir,

Essentially did not mean date date. It could have been a function calculation etc However i wanted to get all the data sets from one library to another library after doing the same calculations on the data sets from the source library and saving it to the destination library

Thank you for your time into looking through my macro

Contributor
Posts: 31

Re: Need help with a simple macro

Posted in reply to Ron_Fehd_macro_maven

Thanks

It should not be date as such i would like to do essentially same calculations on my source library data sets and save it to another destination.

Thanks for looking through the code and your time

Super User
Posts: 5,516

Re: Need help with a simple macro

Posted in reply to rahul88888

Here are a few questions that might cover what you are asking.

How do I get a list of all data set names within a folder?

How do I get a list of all data sets containing OLD_DATE within a folder?

How do I apply my macro for every data set name on a list?

How do I modify my macro to copy a data set from one folder to another?

Narrow down the question a little, and you are sure to get answers.

Contributor
Posts: 65

Re: Need help with a simple macro

Posted in reply to rahul88888

Hi Rahul

If the following is your qurey then find the code followed by the query.

You have some datasets in one location and all the datasets have a date variable(Old_Date with a particular date format).

Now you want to convert the date variable into date9. format and you want to save the dataset with the same name in another location.

filename Source "Source Path";

libname Source "Source Path";

libname Dest "Destination Path";

%macro dates(Dsn);

data Dest.&Dsn;

set Source.&Dsn;

new_date=input(old_DATE,dateformat);

format new_date date9.;

run;

%mend;

*Open the directory and find the number of items in the directory;

data _null_;

did=dopen("source");

dcnt=dnum(did);

call symput("Count",dcnt);

run;

%Macro datasets;

data datasets;

did = dopen("Source");

%do i = 1 %to &Count;

if scan(dread(did,&i),2,'.') eq "sas7bdat" then do;      *Checking whether the filename read by dread() is SAS dataset or not;

Dsn = scan(dread(did,&i),1,'.');     * Reading the datasetname without extension;

call execute('%dates('||strip(Dsn)||')');

end;

%end;

run;

%mend;

%datasets;

New User
Posts: 1

Re: Need help with a simple macro

Posted in reply to MadhuKorni

Why not use Proc Copy?  You would just need to assign two libnames and use select or exclude statements to copy the SAS datasets you want.

Contributor
Posts: 31

Re: Need help with a simple macro

hi

the problem with proc copy is that i don't want to change anything within my code

also I would have to perform some calculations on source data set and save it to destination library. Which will also require me to keep drop variables, concatenate a few and likewise .

All i am trying to do is create a mac file which the user has to give source and destination library and the user should be done with it

The code i have written actually works however the user has to change data set name every time which i don't want to do.

Thanks

Contributor
Posts: 31

Re: Need help with a simple macro

I have tried it using proc copy too it does work

Smiley Happy thank you

Contributor
Posts: 65

Re: Need help with a simple macro

Hi...

He wants to execute some statements while moving the datasets from one directory to other.

Using proc copy we cannot execute statements other than select and exclude.

Solution
‎06-26-2015 05:39 AM
Super User
Super User
Posts: 7,993

Re: Need help with a simple macro

Posted in reply to MadhuKorni

A simpler method would be to use the already existing metadata which SAS provides in the form of the SASHELP.VTABLE (and VCOLUMN):

%macro mCopy (InLib=,OutLib=);

  data _null_;

    set sashelp.vtable (where=(libname=upcase("&InLib.") and memtype="DATA"));

    call execute('data &outlib..'||strip(memname)||'; set &inlib..'||strip(memname)||'; new_date=today(); format new_date date9.; run;');

  run;

%mend mCopy;

%mCopy (InLib=sashelp,OutLib=Work);

Contributor
Posts: 31

Re: Need help with a simple macro

Thank You Smiley Happy

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 427 views
  • 5 likes
  • 6 in conversation