BookmarkSubscribeRSS Feed
lydiawawa
Lapis Lazuli | Level 10

Hi,

I'm importing a parameter file in txt form with no title row such as the following:

byvars             TEST16X GIO
log_transform      N
y_intercept        Y
exclude_outliers   N
exclude_einmos     N

Because it is a parameter file, the length of the two columns will not be fixed. The following is the problematic code I created to import the txt file. The two columns are concatenated instead of splitting into individual columns:

data test1;
	infile "files/parameters.txt" DELIMITER='09'x col=Colpoint
	length=linelen;
	length pname $30 pvalue $10;

	input @1 pname $ @;
	varlen=linelen - colpoint + 1;
    input pvalue $varying1024. varlen;
	call symputx('pname', STRIP(pvalue));
run;

Output:

lydiawawa_0-1673991736794.png

 

 

 

3 REPLIES 3
Astounding
PROC Star

The data you posted uses spaces as delimiters, not tabs.  The program to read it could be as simple as:

 

data want;
   infile "files/parameters.txt";
   length pname $ 30 pvalue $ 10;
   input pname pvalue;
   call symputx(pname, pvalue);
run;

It's not clear why you want macro variables here, and if you don't need them you could remove the last statement.

lydiawawa
Lapis Lazuli | Level 10

This parameter file defines global macro variables and their values. Such that log_transform is a macro variable with value 'N'.

Tom
Super User Tom
Super User

Do you have specifications for how the file was created?

Is the white space between the two values spaces or a single tab character?  Your program is saying it should be a single TAB character, but the text you posted has spaces instead.

 

If it is a tab character then using DSD should work, as long as any PVALUE that has a tab in it is quoted.

data parameters;
  infile "files/parameters.txt" dsd dlm='09'x truncover ;
  input pname :$32. pvalue :$200.;
  call symputx(pname,pvalue);
run;

If the delimiter is a space (or the PVALUE has embedded delimiters without quotes) and parameter name does not contain any spaces then you can just use formatted input for the PVALUE. Using formatted input will read through and delimiters.

data parameters;
  infile "files/parameters.txt" truncover ;
  input pname :$32. pvalue $200.;
  call symputx(pname,pvalue);
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 991 views
  • 3 likes
  • 3 in conversation