BookmarkSubscribeRSS Feed
podarum
Quartz | Level 8

Hi,  I'm trying to figure out a way to deal with a filed from a monthly file that I have to import that is dynamic.  What I mean is the file is the same every month except one field changes name.. out of 6 fileds there is a field wiht the name as a date (that changes every month).. for example from _0111130 (eg. Nov 30, 2011) to _0111231 (eg. Dec 31, 2012).. and I want it to always name it 'Pairs'.....  any way of letting sas know that no matter what that field is called to always name it 'Pairs' ?   thanks

9 REPLIES 9
art297
Opal | Level 21

Is it the only field that begins with an underscore?

podarum
Quartz | Level 8

No, there's 4 others.

art297
Opal | Level 21

Is it the only field that begins with an underscore followed by a number?

podarum
Quartz | Level 8

No, they all do..

_01

_0110831

_0111231

_1

Tom
Super User Tom
Super User

What are you importing from? Do the fields change order? If not then see if you can import without the names and then just rename the automatic variables.

Otherwise query the metadata to find the name of the field that is NOT one of the five known field names.

proc sql noprint ;

   select name into :fieldnm

    from dictionary.columns

    where libname='WORK' and memname='IMPORT'

        and name not in ('ONE','TWO','THREE','FOUR','FIVE')

  ;

quit;

data want ;

   set import(rename=(&fieldnm=SIX));

run;

podarum
Quartz | Level 8

From a .txt file .. the fields will not change order just value

Tom
Super User Tom
Super User

If it is a text file with known variable order why would you use IMPORT to begin with?

Use the FIRSTOBS option on the INFILE statement to skip the column headers.

data want ;

   infile 'myfile.txt' firstobs=2 dsd dlm=',' truncover ;

   input one two three four five six;

art297
Opal | Level 21

I agree with Tom.  However, since I just posted the following as a possible answer to a related question, thought you might want to know about it if you have ALREADY imported all of the data and have SAS files.

libname mydata "c:\art\test";

data mydata.productid;

  set sashelp.class (rename=(sex=productid));

run;

data mydata.salesamount;

  set sashelp.class (rename=(sex=salesamount));

run;

data _null_;

    dsid=open("mydata.productid","i");

    call execute('proc datasets library=mydata nolist;');

    call execute('    modify productid;');

    call execute('    rename '||varname(dsid,2)||'=errorcolumn;');

    call execute('quit;');

    rc=close(dsid);

    dsid=open("mydata.salesamount","i");

    call execute('proc datasets library=mydata nolist;');

    call execute('    modify salesamount;');

    call execute('    rename '||varname(dsid,2)||'=errorcolumn;');

    call execute('quit;');

    rc=close(dsid);

run;

podarum
Quartz | Level 8

Thanks guys.

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
  • 9 replies
  • 978 views
  • 6 likes
  • 3 in conversation