How to display time without colon in sas - sas

how can I display range of time with no colon?
data alltime;
do hr = '00:00:00't to '23:59:59't;
output; end; format hr time8.; run;
02:23:30 => 022330

How about B8601TM6. format?
data _null_;
do hr = '00:00:00't to '23:59:59't by 65*60+23 ;
put hr time8. '->' hr b8601tm6. ;
end;
run;
Results:
0:00:00->000000
1:05:23->010523
2:10:46->021046
3:16:09->031609
4:21:32->042132
5:26:55->052655
6:32:18->063218
7:37:41->073741
8:43:04->084304
9:48:27->094827
10:53:50->105350
11:59:13->115913
13:04:36->130436
14:09:59->140959
15:15:22->151522
16:20:45->162045
17:26:08->172608
18:31:31->183131
19:36:54->193654
20:42:17->204217
21:47:40->214740
22:53:03->225303
23:58:26->235826

it worked out like that:
data alltime;
do hr = '00:00:00't to '23:59:59't;
output; end; format hr b8601tm8.; run;

Related

Using macro variable in an IF statement within a loop is not working

I am having an issue with my code where it is working when I am hard coding the value (in comments) in the IF statement but when I insert the macro variable, the functions 'Copy' and 'Delete' do not work with no errors generated. Below is the code being used:
*%let pathscr = //files/FEB_P000/Reporting_FS;
%let pathdes = //files/FEB_P000/Reporting_FS/Accounting log/2021;
%let fn = LFNPAccounting;
%let dt = %sysfunc(inputn(&acc_date, yymmddn8.),yymmddn8.); /* 20211209 */
%let Var = &fn&dt;/* LFNPAccounting20211209 */
data _null_;
length fref $8 fname $256;
did = filename(fref,'\\files\FEB_P000\Reporting_FS');
did = dopen(fref);
do i = 1 to dnum(did);
fname = dread(did,i);
newfn = SUBSTR(fname,1,22);
if newfn = &Var then do;
/*if newfn = 'LFNPAccounting20211209' then do;*/
rc1=filename('src',catx('/',"&pathscr",fname));
rc2=filename('des',catx('/',"&pathdes",fname));
rc3=fcopy('src','des');
rc4= fdelete('src');
end;
end;
run;*
Could anyone help please?
Thanks
Hans
I am guessing you try to look into a specified folder pathscr, and if a file matches a certain string (SUBSTR(fname,1,22)), you copy and delete the latter to the Logs folder pathdes.
libname report "/home/kermit/temp/Reporting/";
data report.have20211210
report.have20211209
report.have20211208;
id = 1;
output;
run;
%let pathscr = /home/kermit/temp/Reporting/;
%let pathdes = /home/kermit/temp/Logs/;
%let fn = have; /* Name of the file */
%let type = .sas7bdat; /* File extension */
%let dt = %sysfunc(inputn(%sysfunc(today()), yymmddn8.), yymmddn8.);
%let file = &fn&dt&type.;
%put &=file;
data _null_;
drop rc did;
rc=filename("mydir", "&pathscr.");
did=dopen("mydir");
if did > 0 then do; /* check that the directory can be opened */
do i=1 to dnum(did); /* use dnum() to determine the highest possible member number */
fname=dread(did, i); /* get the name of the file */
if fname = "&file." then do; /* if the name of the file match: */
rc=filename('src', "&pathscr&file.");
rc=filename('des', "&pathdes&file.");
rc=fcopy('src', 'des'); /* copy from source to destination */
rc=fdelete('src'); /* delete from source */
end;
end;
end;
else do; /* if directory cannot be open, put the error message to the logs */
msg=sysmsg();
put msg;
end;
run;
Logs:
FILE=have20211210.sas7bdat
DOPEN opens a directory and returns a directory identifier value (a number greater than 0) that is used to identify the open directory in other SAS external file access functions. If the directory cannot be opened, DOPEN returns 0, and you can obtain the error message by calling the SYSMSG function.
I used today() for the dt macro-variable for convenience sake, but you will have to change it to whatever date you are searching for.
Consider that with the code above, if the file is already in the Logs folder, it will not be overwritten. Note that you do not have to use the CATX function if you put another / at the very end of your specified path.
Result
Macro variables are not resolved when bounded by single quotes. They are resolved when within double quotes.
Try
did = filename(fref,"&path_scr");
You set VAR to a value like:
%let Var = LFNPAccounting20211209 ;
Then you use it to generate a SAS statement:
if newfn = &Var then do;
Which will resolve to
if newfn = LFNPAccounting20211209 then do;
Since I did not see you creating any variable named LFNPAccounting20211209 it is most likely that you want to use this statement instead:
if newfn = "&Var" then do;
So that the SAS code you generate will compare the value of NEWFN to a string literal instead of another variable.
Note: Since it looks like you are using WINDOWS filesystem you should make the comparison case insenstive.
if upcase(newfn) = %upcase("&Var") then do;

How do I unequally bin data in SAS?

I am trying to take a data set that ranges from -2.0 to 1.55 and make three bins of the data. The data are z scores that represent lengths, and I'm trying to find cutoffs for "short, medium, long," essentially.
I know this cannot be done using univariate, so I've been playing with the bin statement, but I am having trouble with it.
Here is the data and some code I can't get to work:
data limbs;
input
LowerLimb
;
datalines;
0.5945611665
-0.5826515170
-1.2089586047
-0.7638814175
-1.3541648163
-0.8279052306
-0.9069854423
0.9439623714
-0.1525671573
0.4056990026
1.5466954947
0.4034370839
0.8766515519
-1.5657943810
0.1315781412
0.5884629368
0.6104427011
-0.1874296672
-0.6318866100
-1.0145154507
0.4573267066
-0.0788037696
0.0988716187
-0.1062918576
1.6032740744
0.0366051704
1.2256319114
-0.0975189376
-0.2566850316
1.6652074953
-0.2515183734
0.5004921436
-0.1593883100
0.8129010817
1.1351908320
0.8123843303
1.2870155459
-1.1128448929
0.4506147031
-0.6403088674
-1.0680294390
0.6944292489
-1.5325710123
0.5268637927
1.2873515926
-0.1441695459
-0.7166217143
1.2461334186
-0.9583531596
-0.7342533139
-0.4907715810
0.2059216422
-1.3839801362
0.7310499731
0.6130932991
1.0859079024
1.4255534497
-0.1813774454
0.6544726467
-1.0171713430
-0.5005970523
0.5884629368
-1.4752458285
-0.9195150817
-1.4752458285
0.8515222486
-0.6348874123
-1.0206723355
-0.3331377791
-1.1015990720
-0.1196299907
-0.3504059025
-0.5797983640
-0.2784242647
0.4381186749
0.4665127006
-1.8605760577
-1.5943485819
-0.4196862951
0.5247889197
1.6982671983
-0.5015070356
0.2510690218
-0.1088654424
-0.0470926244
-1.1256998568
-0.4694781522
-0.4903309954
-0.1706456902
-0.7996053224
-0.2106636370
1.1087050595
1.5393390992
0.6407710538
0.8738320036
-1.1218388138
0.5477816746
0.5999120789
0.2915917178
0.5932996471
-0.4754278117
-0.1195030573
0.3480903069
0.1629924791
-0.8543653798
0.0602221361
-0.3484280234
0.8213886228
1.0996879917
-1.0171713430
-0.2613938856
0.1435928118
-0.2410397237
2.0380301721
0.9942206208
-0.7858668669
1.0463609814
0.5651396814
-0.4366703308
-1.2232641582
-0.3770888329
-1.9197016431
1.0463609814
-1.3738499052
-1.0554234361
1.1701816705
-0.8687068897
-0.8743902197
-1.3518493892
-1.6473112739
-0.2953961077
0.5734156662
0.5065516647
1.1603237185
-0.3369092077
1.0982075159
-1.0002141384
-0.9192524613
-0.0431072738
-0.0742208903
0.8658302777
-1.1095158202
-0.8361540961
0.5871263103
-0.3311134236
0.3331929252
-0.6499008335
-1.1966097379
0.7227541366
0.1853978157
0.8074323856
-0.8096153897
-1.0220042319
1.1172583088
1.3540629514
-1.3149667205
-0.7600725098
1.1145492382
1.3270625584
1.1572834877
-1.1877623250
0.3202875975
0.8779400227
-0.4333817521
1.2656618368
-1.1416425163
-0.9014599711
0.3918324501
-0.7997140876
-0.2229835864
-0.0362833762
-0.4399531798
1.1975110853
-0.0183032379
-0.7413393186
0.8474043498
1.2789829755
0.8673767628
-0.4438902513
1.0776590402
0.1910287517
1.1313548102
0.8659515949
-0.9444619985
-0.9926366647
0.6447604307
0.8370824694
-0.8917575544
1.5862615371
0.8437626048
0.2362696149
-1.1429415083
-1.2621422188
-0.7364910931
0.3618265073
0.1708182871
-0.3114446248
0.0011119450
-1.0323790009
0.5951509779
1.6392758858
0.9646250229
-0.9076823320
-0.1409210592
-0.8529359998
;
cutpts = do(-2.00, 1.55);
b = bin(x, 3); /* i_th element is 1-8 to indicate bin */
If you want to group the data into 3 groups you can use PROC RANK with GROUP=3. You don't have to sort by the target variable but I makes it easier to see what was done.
data limbs;
input LowerLimb ##;
datalines;
0.5945611665 -0.5826515170 -1.2089586047 -0.7638814175 -1.3541648163 -0.8279052306 -0.9069854423 0.9439623714 -0.1525671573 0.4056990026
1.5466954947 0.4034370839 0.8766515519 -1.5657943810 0.1315781412 0.5884629368 0.6104427011 -0.1874296672 -0.6318866100 -1.0145154507
0.4573267066 -0.0788037696 0.0988716187 -0.1062918576 1.6032740744 0.0366051704 1.2256319114 -0.0975189376 -0.2566850316 1.6652074953
-0.2515183734 0.5004921436 -0.1593883100 0.8129010817 1.1351908320 0.8123843303 1.2870155459 -1.1128448929 0.4506147031 -0.6403088674
-1.0680294390 0.6944292489 -1.5325710123 0.5268637927 1.2873515926 -0.1441695459 -0.7166217143 1.2461334186 -0.9583531596 -0.7342533139
-0.4907715810 0.2059216422 -1.3839801362 0.7310499731 0.6130932991 1.0859079024 1.4255534497 -0.1813774454 0.6544726467 -1.0171713430
-0.5005970523 0.5884629368 -1.4752458285 -0.9195150817 -1.4752458285 0.8515222486 -0.6348874123 -1.0206723355 -0.3331377791 -1.1015990720
-0.1196299907 -0.3504059025 -0.5797983640 -0.2784242647 0.4381186749 0.4665127006 -1.8605760577 -1.5943485819 -0.4196862951 0.5247889197
1.6982671983 -0.5015070356 0.2510690218 -0.1088654424 -0.0470926244 -1.1256998568 -0.4694781522 -0.4903309954 -0.1706456902 -0.7996053224
-0.2106636370 1.1087050595 1.5393390992 0.6407710538 0.8738320036 -1.1218388138 0.5477816746 0.5999120789 0.2915917178 0.5932996471
-0.4754278117 -0.1195030573 0.3480903069 0.1629924791 -0.8543653798 0.0602221361 -0.3484280234 0.8213886228 1.0996879917 -1.0171713430
-0.2613938856 0.1435928118 -0.2410397237 2.0380301721 0.9942206208 -0.7858668669 1.0463609814 0.5651396814 -0.4366703308 -1.2232641582
-0.3770888329 -1.9197016431 1.0463609814 -1.3738499052 -1.0554234361 1.1701816705 -0.8687068897 -0.8743902197 -1.3518493892 -1.6473112739
-0.2953961077 0.5734156662 0.5065516647 1.1603237185 -0.3369092077 1.0982075159 -1.0002141384 -0.9192524613 -0.0431072738 -0.0742208903
0.8658302777 -1.1095158202 -0.8361540961 0.5871263103 -0.3311134236 0.3331929252 -0.6499008335 -1.1966097379 0.7227541366 0.1853978157
0.8074323856 -0.8096153897 -1.0220042319 1.1172583088 1.3540629514 -1.3149667205 -0.7600725098 1.1145492382 1.3270625584 1.1572834877
-1.1877623250 0.3202875975 0.8779400227 -0.4333817521 1.2656618368 -1.1416425163 -0.9014599711 0.3918324501 -0.7997140876 -0.2229835864
-0.0362833762 -0.4399531798 1.1975110853 -0.0183032379 -0.7413393186 0.8474043498 1.2789829755 0.8673767628 -0.4438902513 1.0776590402
0.1910287517 1.1313548102 0.8659515949 -0.9444619985 -0.9926366647 0.6447604307 0.8370824694 -0.8917575544 1.5862615371 0.8437626048
0.2362696149 -1.1429415083 -1.2621422188 -0.7364910931 0.3618265073 0.1708182871 -0.3114446248 0.0011119450 -1.0323790009 0.5951509779
1.6392758858 0.9646250229 -0.9076823320 -0.1409210592 -0.8529359998
;;;;
run;
proc sort data=limbs;
by lowerLimb;
run;
proc rank data=limbs out=bin groups=3;
var lowerLimb;
ranks bin;
run;

SAS: Delete all .txt files in a folder

I need to delete all text files form a directory. The following program works fine for the file listed (eg:file.txt), but when I try to use *.txt it doesn't work. Am I missing something or is there a better way to delete all txt files in a directory.
data _null_;
fname = "_files";
rc = filename(fname,"&path\file.txt");
if rc = 0 and fexist(fname) then
rc = fdelete(fname);
rc = filename(fname);
run;
If you are a fan of macros.. the code below should do the same.
options mlogic;
%macro delete_all_txt_files_in_folder(folder);
filename filelist "&folder";
data _null_;
dir_id = dopen('filelist');
total_members = dnum(dir_id);
do i = 1 to total_members;
member_name = dread(dir_id,i);
if scan(lowcase(member_name),2,'.')='txt' then do;
file_id = mopen(dir_id,member_name,'i',0);
if file_id > 0 then do;
freadrc = fread(file_id);
rc = fclose(file_id);
rc = filename('delete',member_name,,,'filelist');
rc = fdelete('delete');
end;
rc = fclose(file_id);
end;
end;
rc = dclose(dir_id);
run;
%mend;
%delete_all_txt_files_in_folder(C:\try)
You can't use a wildcard with fdelete. You either need to loop over all of the files in the directory, or you can use an x command
x 'del &path.\*.txt';
or similar depending on your OS (but it is OS dependent, and requires XCMD permission.
Here's the loop:
%let path=d:\temp;
filename filrf "&path.";
data _null_;
did = dopen('filrf');
memcount = dnum(did);
do while (memcount>0);
fname = dread(did,memcount);
if scan(lowcase(fname),2,'.')='txt' then do;
rcref = filename('fref',catx('\',"&path.",fname));
rcdel = fdelete('fref');
end;
memcount+-1;
end;
stop;
run;

Retrieving tables located on metadata server with SAS

I want to list all tables on metadata server. I've tried with the following data step but it only pics one table per library and I can't figure out why.
This is the code I've been using:
options metaserver="xxxx"
metaport=8561
metauser="xxxx"
metapass="xxxx"
METAPROTOCOL=BRIDGE
metarepository="Foundation";
data meta_libraries;
length uri serveruri conn_uri domainuri libname ServerContext AuthDomain path_schema
usingpkguri type tableuri coluri $256 id $17
desc $200 libref engine $8 isDBMS $1 table colname coltype collen $32;
keep libname desc libref engine ServerContext path_schema AuthDomain table colname coltype collen
IsPreassigned IsDBMSLibname id;
nobj=.;
n=1;
uri='';
serveruri='';
conn_uri='';
domainuri='';
/***Determine how many libraries there are***/
nobj=metadata_getnobj("omsobj:SASLibrary?#Id contains '.'",n,uri);
/***Retrieve the attributes for all libraries, if there are any***/
if n>0 then do n=1 to nobj;
libname='';
ServerContext='';
AuthDomain='';
desc='';
libref='';
engine='';
isDBMS='';
IsPreassigned='';
IsDBMSLibname='';
path_schema='';
usingpkguri='';
type='';
id='';
nobj=metadata_getnobj("omsobj:SASLibrary?#Id contains '.'",n,uri);
rc= metadata_getattr(uri, "Name", libname);
rc= metadata_getattr(uri, "Desc", desc);
rc= metadata_getattr(uri, "Libref", libref);
rc= metadata_getattr(uri, "Engine", engine);
/*rc= metadata_getattr(uri, "IsDBMSLibname", isDBMS);*/
rc= metadata_getattr(uri, "IsDBMSLibname", IsDBMSLibname);
rc= metadata_getattr(uri, "IsPreassigned", IsPreassigned);
rc= metadata_getattr(uri, "Id", Id);
/*** Get associated ServerContext ***/
i=1;
rc= metadata_getnasn(uri, "DeployedComponents", i, serveruri);
if rc > 0 then rc2= metadata_getattr(serveruri, "Name", ServerContext);
else ServerContext='';
/*** If the library is a DBMS library, get the Authentication Domain
associated with the DBMS connection credentials ***/
if isDBMS="1" then do;
i=1;
rc= metadata_getnasn(uri, "LibraryConnection", i, conn_uri);
if rc > 0 then do;
rc2= metadata_getnasn(conn_uri, "Domain", i, domainuri);
if rc2 > 0 then rc3= metadata_getattr(domainuri, "Name", AuthDomain);
end;
end;
/*** Get the path/database schema for this library ***/
rc=metadata_getnasn(uri, "UsingPackages", 1, usingpkguri);
if rc>0 then do;
rc=metadata_resolve(usingpkguri,type,id);
if type='Directory' then rc=metadata_getattr(usingpkguri, "DirectoryName", path_schema);
else if type='DatabaseSchema' then rc=metadata_getattr(usingpkguri, "Name", path_schema);
else path_schema="unknown";
end;
/*** Get the tables associated with this library ***/
/*** If DBMS, tables are associated with DatabaseSchema ***/
if type='DatabaseSchema' then do;
t=1;
ntab=metadata_getnasn(usingpkguri, "Tables", t, tableuri);
if ntab>0 then do t=1 to ntab;
tableuri='';
table='';
ntab=metadata_getnasn(usingpkguri, "Tables", t, tableuri);
tabrc= metadata_getattr(tableuri, "Name", table);
output;
end;
else do;
put 'Library ' libname ' has no tables registered';
output;
end;
end;
end;
else if type='Directory' then do;
t=1;
ntab=metadata_getnasn(uri, "Tables", t, tableuri);
if ntab>0 then do t=1 to ntab;
tableuri='';
table='';
ntab=metadata_getnasn(uri, "Tables", t, tableuri);
tabrc= metadata_getattr(tableuri, "Name", table);
output;
end;
else put 'Library ' libname ' has no tables registered';
end;
/***If there aren't any libraries, write a message to the log***/
else put 'There are no libraries defined in this metadata repository.';
run;
I've would appreciate if someone has any suggestions?
I would recommend you to use "proc metadata" instead of the data step functions.
This is a possible solution although it can be improved since it is bringing more information than it should.
options metaport = &metaport
metaserver = "&metaserver"
metarepository = "Foundation"
metauser = &meta_user.
metapass = "&meta_pass";
* use a temporary fileref to hold the request;
filename request temp;
* use a data step to create the XML request;
data _null_;
file request;
put '<GetMetadataObjects>';
put ' <Reposid>$METAREPOSITORY</Reposid>';
put ' <Type>PhysicalTable</Type>';
put ' <Objects />';
put ' <NS>SAS</NS>';
* Flags: <!-- OMI_ALL(1) + OMI_GET_METADATA(256) + OMI_TEMPLATE (4)+ OMI_SUCCINT (2048) = 2309 -->;
put ' <Flags>2309</Flags>';
put '<Options>';
put ' <Templates>';
put ' <PhysicalTable/>';
put ' <SASLibrary Engine="" Libref=""/>';
put ' </Templates>';
put ' </Options>';
put '</GetMetadataObjects>';
run;
* use a temporary fileref to hold the response;
filename response TEMP; *"%sysfunc(getoption(work))/response.xml";
proc metadata in=request out=response verbose;
run;
* release the temporary request;
filename request;
filename mapa temp;
data _null_;
file mapa;
put '<?xml version="1.0" encoding="windows-1252"?>';
put '<!-- ############################################################ -->';
put '<!-- 2014-02-05T15:41:54 -->';
put '<!-- SAS XML Libname Engine Map -->';
put '<!-- Generated by XML Mapper, 903200.3.0.20120523190000_v930m2 -->';
put '<!-- ############################################################ -->';
put '<SXLEMAP name="AUTO_GEN" version="2.1">';
put ' <NAMESPACES count="0"/>';
put ' <!-- ############################################################ -->';
put ' <TABLE description="SASLibrary" name="SASLibrary">';
put ' <TABLE-PATH syntax="XPath">/GetMetadataObjects/Objects/PhysicalTable/TablePackage/SASLibrary</TABLE-PATH>';
put ' <COLUMN name="LibId">';
put ' <PATH syntax="XPath">/GetMetadataObjects/Objects/PhysicalTable/TablePackage/SASLibrary/#Id</PATH>';
put ' <TYPE>character</TYPE>';
put ' <DATATYPE>string</DATATYPE>';
put ' <LENGTH>17</LENGTH>';
put ' </COLUMN>';
put ' <COLUMN name="Engine">';
put ' <PATH syntax="XPath">/GetMetadataObjects/Objects/PhysicalTable/TablePackage/SASLibrary/#Engine</PATH>';
put ' <TYPE>character</TYPE>';
put ' <DATATYPE>string</DATATYPE>';
put ' <LENGTH>4</LENGTH>';
put ' </COLUMN>';
put ' <COLUMN name="Libref">';
put ' <PATH syntax="XPath">/GetMetadataObjects/Objects/PhysicalTable/TablePackage/SASLibrary/#Libref</PATH>';
put ' <TYPE>character</TYPE>';
put ' <DATATYPE>string</DATATYPE>';
put ' <LENGTH>8</LENGTH>';
put ' </COLUMN>';
put ' <COLUMN name="TabID" retain="YES">';
put ' <PATH syntax="XPath">/GetMetadataObjects/Objects/PhysicalTable/#Id</PATH>';
put ' <TYPE>character</TYPE>';
put ' <DATATYPE>string</DATATYPE>';
put ' <LENGTH>17</LENGTH>';
put ' </COLUMN>';
put ' <COLUMN name="Name_Table" retain="YES">';
put ' <PATH syntax="XPath">/GetMetadataObjects/Objects/PhysicalTable/#Name</PATH>';
put ' <TYPE>character</TYPE>';
put ' <DATATYPE>string</DATATYPE>';
put ' <LENGTH>32</LENGTH>';
put ' </COLUMN>';
put ' <COLUMN name="SASTableName" retain="YES">';
put ' <PATH syntax="XPath">/GetMetadataObjects/Objects/PhysicalTable/#SASTableName</PATH>';
put ' <TYPE>character</TYPE>';
put ' <DATATYPE>string</DATATYPE>';
put ' <LENGTH>32</LENGTH>';
put ' </COLUMN>';
put ' <COLUMN name="TableName" retain="YES">';
put ' <PATH syntax="XPath">/GetMetadataObjects/Objects/PhysicalTable/#TableName</PATH>';
put ' <TYPE>character</TYPE>';
put ' <DATATYPE>string</DATATYPE>';
put ' <LENGTH>32</LENGTH>';
put ' </COLUMN>';
put ' </TABLE>';
put '</SXLEMAP>';
run;
libname fim xmlv2 xmlfileref=response xmlmap=MAPA access=READONLY;
/*
* Local Extraction
*/
DATA SASLibrary; SET fim.SASLibrary; run;
* release the temporary file and libname;
filename response clear;
libname fim clear;
Your else if type='Directory' then do; line was outside of the necessary do; end; group.
Other than that, your code runs fine! See below.
data meta_libraries;
length uri serveruri conn_uri domainuri libname ServerContext AuthDomain path_schema
usingpkguri type tableuri coluri $256 id $17
desc $200 libref engine $8 isDBMS $1 table colname coltype collen $32;
keep libname desc libref engine ServerContext path_schema AuthDomain table colname
coltype collen IsPreassigned IsDBMSLibname id;
nobj=.;
n=1;
uri='';
serveruri='';
conn_uri='';
domainuri='';
/***Determine how many libraries there are***/
nobj=metadata_getnobj("omsobj:SASLibrary?#Id contains '.'",n,uri);
/***Retrieve the attributes for all libraries, if there are any***/
if n>0 then do n=1 to nobj;
libname='';
ServerContext='';
AuthDomain='';
desc='';
libref='';
engine='';
isDBMS='';
IsPreassigned='';
IsDBMSLibname='';
path_schema='';
usingpkguri='';
type='';
id='';
nobj=metadata_getnobj("omsobj:SASLibrary?#Id contains '.'",n,uri);
rc= metadata_getattr(uri, "Name", libname);
rc= metadata_getattr(uri, "Desc", desc);
rc= metadata_getattr(uri, "Libref", libref);
rc= metadata_getattr(uri, "Engine", engine);
rc= metadata_getattr(uri, "IsDBMSLibname", isDBMS);
rc= metadata_getattr(uri, "IsDBMSLibname", IsDBMSLibname);
rc= metadata_getattr(uri, "IsPreassigned", IsPreassigned);
rc= metadata_getattr(uri, "Id", Id);
/*** Get associated ServerContext ***/
i=1;
rc= metadata_getnasn(uri, "DeployedComponents", i, serveruri);
if rc > 0 then rc2= metadata_getattr(serveruri, "Name", ServerContext);
else ServerContext='';
/*** If the library is a DBMS library, get the Authentication Domain
associated with the DBMS connection credentials ***/
if isDBMS="1" then do;
i=1;
rc= metadata_getnasn(uri, "LibraryConnection", i, conn_uri);
if rc > 0 then do;
rc2= metadata_getnasn(conn_uri, "Domain", i, domainuri);
if rc2 > 0 then rc3= metadata_getattr(domainuri, "Name", AuthDomain);
end;
end;
/*** Get the path/database schema for this library ***/
rc=metadata_getnasn(uri, "UsingPackages", 1, usingpkguri);
if rc>0 then do;
rc=metadata_resolve(usingpkguri,type,id);
if type='Directory' then
rc=metadata_getattr(usingpkguri, "DirectoryName", path_schema);
else if type='DatabaseSchema' then
rc=metadata_getattr(usingpkguri, "Name", path_schema);
else path_schema="unknown";
end;
/*** Get the tables associated with this library ***/
/*** If DBMS, tables are associated with DatabaseSchema ***/
if type='DatabaseSchema' then do;
t=1;
ntab=metadata_getnasn(usingpkguri, "Tables", t, tableuri);
if ntab>0 then do t=1 to ntab;
tableuri='';
table='';
ntab=metadata_getnasn(usingpkguri, "Tables", t, tableuri);
tabrc= metadata_getattr(tableuri, "Name", table);
output;
end;
else do;
put 'Library ' libname ' has no tables registered';
output;
end;
end;
else if type='Directory' then do;
t=1;
ntab=metadata_getnasn(uri, "Tables", t, tableuri);
if ntab>0 then do t=1 to ntab;
tableuri='';
table='';
ntab=metadata_getnasn(uri, "Tables", t, tableuri);
tabrc= metadata_getattr(tableuri, "Name", table);
output;
end;
else put 'Library ' libname ' has no tables registered';
end;
end;
/***If there aren't any libraries, write a message to the log***/
else put 'There are no libraries defined in this metadata repository.';
run;
proc sort; by libref table;run;

Fetch user id from SAS metadata server

I am working in a configuration that uses the IOM to connect to the metadata server - hence there are no automatic macro variables in my environment to determine the user id (we are using a pooled workspace server with a generic host account).
Is there a short piece of code which can be used to query the metadata server for the SAS user id?
The following is quite long winded, and could probably be shortened - but it does the job!
data _null_;
call symput('login_id',''); /* initialise to missing */
n = 1;
length loginUri person $ 512;
nobj = metadata_getnobj("omsobj:Login?*",n, loginUri);
if (nobj>0) then do;
length __uri __objName __porig personUri $256;
__porig = loginUri;
__uri = '';
__objName = '';
__n = 1;
__objectFound = 0;
personUri = "";
__numObjs = metadata_getnasn(__porig ,"AssociatedIdentity", 1, __uri);
do until(__n > __numObjs | __objectFound );
__rc = metadata_getattr(__uri, "PublicType", __objName);
if __objName="User" then do;
__rc=metadata_getattr(__uri, "Name", __objName);
__objectFound = 1;
personUri = __uri;
end;
else do;
__n = __n+1;
rc = metadata_getnasn(__porig, "AssociatedIdentity", __n, __uri);
end;
end;
if upcase("N")="Y" and not __objectFound then do;
put "*ERROR*: Object with PublicType=" "User" " not found for parent " loginUri " under AssociatedIdentity association";
stop;
end;
;
rc = metadata_getattr(personUri, "Name", person);
call symput("login_id", trim(person));
end;
run;
%put &login_id;